Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 3, 2012 18:14
Show Gist options
  • Save davybrion/3611636 to your computer and use it in GitHub Desktop.
Save davybrion/3611636 to your computer and use it in GitHub Desktop.
code snippets for "Sending NHibernate Entities Over The WCF Wire" post
[ServiceContract]
public interface ICustomerService
{
[UseNetDataContractSerializer]
[OperationContract]
IList<Customer> GetCustomersWithTheirOrders();
[UseNetDataContractSerializer]
[OperationContract]
void PersistCustomer(Customer customer);
}
public class CustomerService : ICustomerService
{
private static readonly ISessionFactory _sessionFactory;
static CustomerService()
{
try
{
Configuration configuration = new Configuration().AddAssembly("Northwind.Domain");
_sessionFactory = configuration.BuildSessionFactory();
}
catch (Exception e)
{
Console.Write(e);
throw;
}
}
public IList<Customer> GetCustomersWithTheirOrders()
{
using (ISession session = _sessionFactory.OpenSession())
{
return session.CreateCriteria(typeof(Customer))
.SetFetchMode("Orders", FetchMode.Join)
.SetResultTransformer(CriteriaUtil.DistinctRootEntity)
.List<Customer>();
}
}
public void PersistCustomer(Customer customer)
{
using (ISession session = _sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.SaveOrUpdate(customer);
session.Flush();
transaction.Commit();
}
}
}
}
<system.serviceModel>
<services>
<service name="ServiceImplementation.CustomerService" behaviorConfiguration="customerServiceBehavior">
<endpoint contract="ServiceInterface.ICustomerService" binding="wsHttpBinding"
bindingConfiguration="customerServiceBinding" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/CustomerService"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="customerServiceBinding" maxReceivedMessageSize="2147483647" />
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="customerServiceBehavior" >
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
private static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CustomerService)))
{
host.Open();
Console.WriteLine("press ENTER to quit");
Console.ReadLine();
}
}
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="customerServiceBinding" maxReceivedMessageSize="2147483647" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/CustomerService" binding="wsHttpBinding" name="CustomerServiceEndPoint"
bindingConfiguration="customerServiceBinding" contract="ServiceInterface.ICustomerService" />
</client>
</system.serviceModel>
private static void Main(string[] args)
{
ChannelFactory<ICustomerService> factory = new ChannelFactory<ICustomerService>("CustomerServiceEndPoint");
ICustomerService proxy = factory.CreateChannel();
IList<Customer> customers = proxy.GetCustomersWithTheirOrders();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment