Created
September 3, 2012 18:14
-
-
Save davybrion/3611636 to your computer and use it in GitHub Desktop.
code snippets for "Sending NHibernate Entities Over The WCF Wire" post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ServiceContract] | |
public interface ICustomerService | |
{ | |
[UseNetDataContractSerializer] | |
[OperationContract] | |
IList<Customer> GetCustomersWithTheirOrders(); | |
[UseNetDataContractSerializer] | |
[OperationContract] | |
void PersistCustomer(Customer customer); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void Main(string[] args) | |
{ | |
using (ServiceHost host = new ServiceHost(typeof(CustomerService))) | |
{ | |
host.Open(); | |
Console.WriteLine("press ENTER to quit"); | |
Console.ReadLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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