平时很少写博文的,以前都是转载其他园友的文章,这几天有时间就自己尝试写一些wcf相关的文章,希望能给有需要的人带来一点帮助吧,水平有限再加上初次动手,写得不好还请多多包含!废话不多说了直接进入正题。
1.首先是项目结构(截图)命名不是很规范——仅自己练手用的
注意:WcfInterface是wcf的服务接口
ProductService是wcf的服务的实现
ProductSVCWebApp服务webapp
productservice.svc通过net.tcp绑定协议对外发布服务
messageservice.svc通过net.msmq绑定协议对外发布服务
主要是通过web.config中的servicemodel配置节点实现,该节点中主要分为3部分完全可通过手工配置:bindings绑定、behaviors行为、services服务配置对外发布的服务终结点endpoint——详情请见以下代码
ServiceModel是相关的数据契约
一、wcfinterface项目是独立的服务契约
1.IMessage服务接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.ServiceModel; 7 using ServiceModel; 8 9 namespace WcfInterface 10 { 11 [ServiceContract] 12 public interface IMessage 13 { 14 [OperationContract(IsOneWay=true)] 15 void Send(Message log); 16 } 17 }
2.IServiceTest服务接口
1 using ServiceModel; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.ServiceModel; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace WcfInterface 10 { 11 [ServiceContract] 12 public interface IServiceTest 13 { 14 [OperationContract] 15 int AddProduct(Product product); 16 17 [OperationContract] 18 Product GetProductById(int Id); 19 20 } 21 }
二、ProductService类库
1.IMessage的服务实现
1 using ServiceModel; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using WcfInterface; 8 9 namespace ProductService 10 { 11 public class MsmqServiceTest:IMessage 12 { 13 private static IList<Message> list = new List<Message>(); 14 15 public void Send(ServiceModel.Message log) 16 { 17 list.Add(log); 18 } 19 } 20 }
2.IServiceTest服务
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using WcfInterface; 7 using ServiceModel; 8 9 10 namespace ProductService 11 { 12 public class ProductImpl : IServiceTest 13 { 14 private static IList<Product> list = new List<Product>(); 15 public int AddProduct(Product product) 16 { 17 list.Add(product); 18 return 1; 19 } 20 21 public Product GetProductById(int Id) 22 { 23 return list.FirstOrDefault(p => p.Id.Equals(Id)); 24 } 25 26 27 } 28 }
三、数据契约类库ServiceModel
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.Serialization; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace ServiceModel 9 { 10 [DataContract] 11 public class Product 12 { 13 [DataMember] 14 public int Id { get; set; } 15 [DataMember] 16 public string Name { get; set; } 17 [DataMember] 18 public decimal Cost { get; set; } 19 } 20 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Runtime.Serialization; 7 8 namespace ServiceModel 9 { 10 [DataContract] 11 public class Message 12 { 13 [DataMember] 14 public int Id { get; set; } 15 16 [DataMember] 17 public string Header { get; set; } 18 19 [DataMember] 20 public string Body { get; set; } 21 } 22 }
四、ProductSVCWebapp
productservice.svc服务文件
<%@ ServiceHost Language="C#" Debug="true" Service="ProductService.MsmqServiceTest" %>
messageservice.svc服务文件
<%@ ServiceHost Language="C#" Debug="true" Service="ProductService.MsmqServiceTest" %>
web.config中servicemodel配置
1 <system.serviceModel> 2 <services> 3 <service behaviorConfiguration="endpointbinding" name="ProductService.ProductImpl"> 4 <endpoint address="net.tcp://localhost/ProductService/ProductService.svc" binding="netTcpBinding" bindingConfiguration="tcpbindingtest" name="netTcpBinding_IServiceTest" contract="WcfInterface.IServiceTest"></endpoint> 5 </service> 6 <service name="ProductService.MsmqServiceTest"> 7 <endpoint address="net.msmq://localhost/private/ProductService/ProductService.svc" binding="netMsmqBinding" contract="WcfInterface.IMessage" bindingConfiguration="msmqbindingtest" name="netMsmqBinding_IMessage"></endpoint> 8 </service> 9 </services> 10 <bindings> 11 <netMsmqBinding> 12 <binding name="msmqbindingtest" exactlyOnce="false"> 13 <security> 14 <transport msmqProtectionLevel="None" msmqAuthenticationMode="None"/> 15 <message clientCredentialType="None"/> 16 </security> 17 </binding> 18 </netMsmqBinding> 19 <netTcpBinding> 20 <binding name="tcpbindingtest"> 21 <security mode="None"> 22 <transport protectionLevel="None" clientCredentialType="None"></transport> 23 <message clientCredentialType="None" /> 24 </security> 25 </binding> 26 </netTcpBinding> 27 </bindings> 28 <behaviors> 29 <serviceBehaviors> 30 <behavior name="endpointbinding"> 31 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 32 <serviceDebug includeExceptionDetailInFaults="false" /> 33 </behavior> 34 <behavior name=""> 35 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 36 <serviceDebug includeExceptionDetailInFaults="false" /> 37 </behavior> 38 </serviceBehaviors> 39 </behaviors> 40 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 41 multipleSiteBindingsEnabled="true" /> 42 </system.serviceModel>
由于我这里用到msmq,建立私有的消息队列名字和address="net.msmq://localhost/private/ProductService/ProductService.svc"里的名字对应起来
五、部署——IIS承载服务及iis中相关配置
服务app以及web站点都需要启用协议net.msmq/net.tcp,另外服务还需要添加msmq和tcp的绑定
我的iis配置ProductService服务建立在defaultweb site下默认的defaultweb site已经添加了net.tcp和net.msmq,http绑定等
testwcf部署web站点
服务运行截图:
本人QQ:443813032 如有不妥之处,还请指正,互相学习,共同进步!!!近期研究go lang中,go将超过java,c成为未来10年最流行的语言,有人说它是20世纪的c语言!希望更多的人加入,一起将go发扬光大!!!