结构:
1 using System.ServiceModel; 2 namespace MyServices 3 { 4 [ServiceContract] 5 public interface IHomeService 6 { 7 [OperationContract] 8 int GetLength(string name); 9 } 10 }
1 namespace MyServices 2 { 3 public class HomeService:IHomeService 4 { 5 public int GetLength(string name) 6 { 7 return name.Length; 8 } 9 } 10 }
1 using System; 2 using System.ServiceModel; 3 4 namespace MyServices 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 using (ServiceHost host = new ServiceHost(typeof(HomeService))) 11 { 12 try 13 { 14 host.Open(); 15 Console.WriteLine("服务开启!"); 16 Console.Read(); 17 } 18 catch (Exception e) 19 { 20 Console.WriteLine(e.Message); 21 } 22 } 23 } 24 } 25 }
1 using System; 2 namespace ConsoleApplication1 3 { 4 class Program 5 { 6 static void Main(string[] args) 7 { 8 HomeServiceReference.HomeServiceClient client = new HomeServiceReference.HomeServiceClient(); 9 //HomeServiceClient homeServiceClient = new HomeServiceClient(); 10 var r = client.GetLength("abc"); 11 Console.WriteLine(r); 12 Console.ReadKey(); 13 } 14 } 15 }
配置:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name="IHomeServiceBinding" /> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="MyService.HomeService"> <endpoint address="http://127.0.0.1:1920/HomeService" binding="basicHttpBinding" contract="MyService.IHomeService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://127.0.0.1:1920"/> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="mxbehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="MyService.HomeService" behaviorConfiguration="mxbehavior"> <endpoint address="net.tcp://localhost:1921/HomeService" binding="netTcpBinding" contract="MyService.IHomeService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <host> <baseAddresses> <add baseAddress="http://localhost:1921/HomeService"/> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="mxbehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <netMsmqBinding> <binding name="msmqbinding"> <security mode="None"/> </binding> </netMsmqBinding> </bindings> <services> <service name="MyService.HomeService" behaviorConfiguration="mxbehavior"> <endpoint address="net.msmq://localhost/private/homequeue" binding="netMsmqBinding" contract="MyService.IHomeService" bindingConfiguration="msmqbinding"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:1922/HomeService"/> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>