WCF 学习点滴
- 学习并推荐博客:楼主太给力了
-
HTTP 错误 404.3 - Not Found
由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
解决办法:以管理员运行命令:C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe -i
-
HTTP 错误 500.21 - Internal Server Error
处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
解决办法:
原因:在安装Framework v4.0之后,再启用IIS,导致Framework没有完全安装
解决:开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行->%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
如果还不行,可检查IIS的应用程序池,是否使用集成模式,如果不是则改成集成模式
-
错误信息:此操作将死锁,因为在当前邮件完成处理以前无法收到答复。如果要允许无序的邮件处理,则在 ServiceBehaviorAttribute 上指定可重输入的或多个 ConcurrencyMode
产生的环境:双工协定中。
解决方法:在服务端实现契约的类指定ServiceBehaviorAttribute的ConcurrencyMode为Multiple。
在该类名上增加[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]即可。
-
WCF 学习资源http://www.cnblogs.com/artech/archive/2007/09/15/893838.html
- 服务端配置文件参数理解:
View Code
1 <system.serviceModel> 2 <services> 3 <service name="WcfHelloDemo.Service.Hello" behaviorConfiguration="serviceMetaData"> 4 <endpoint address="http://127.0.0.1:8888/hello" binding="wsHttpBinding" contract="WcfHelloDemo.Contract.IHello"> 5 </endpoint> 6 </service> 7 </services> 8 <behaviors> 9 <serviceBehaviors> 10 <behavior name="serviceMetaData"> 11 <serviceMetadata httpGetEnabled="True" httpGetUrl="http://127.0.0.1:8888/hello/metadata"/> 12 </behavior> 13 </serviceBehaviors> 14 </behaviors> 15 </system.serviceModel>
其中,contract和name要对应到类名以及命名空间,大小写需要匹配.address是获取服务的地址,是自定义的,貌似不需要严格的限制,格式正确即可.
- 如果您发布的WCF服务是net.tcp的传输方式,而且你的IIS是XP或者Win2003的,那么只能通过控制台的Host方式进发布服务了。因为Net.tcp的寄宿方式是需要IIS7以上的版本才可以。
- Service方法重载问题,方法同名时,生成的Metadata会出问题,使用 OperationContract的name属性如 [OperationContract(Name = "AddWithTwoOperands")]
double Add(double x, double y);
[OperationContract(Name = "AddWithThreeOperands")]
double Add(double x, double y, double z); 可实现重载,只不过在client是看不到重载的,看到的是Name的方法名.
有回调函数时的死锁问题: - One-way可以避免TimeoutException的原因和解决方案,但是方法不能够有返回值.通过操作契约的IsOneWay参数设置为true实现异步调用.false实现同步调用.
- 在Service中开辟新的Thread调用CallBack
- System.Threading.SynchronizationContext,SynchonizationContext是为了解决线程关联性问题而设计的
public virtual void Post(SendOrPostCallback d, object state)
public virtual void Send(SendOrPostCallback d, object state) Send和Post分别以同步和异步的方式将以Delegate表示的具体的操作和SynchonizationContext对象对应的Thread关联,而SendOrPostCallback delegate对象代表你需要的线程关联操作,state代表传入delegate的参数: [CallbackBehavior(UseSynchronizationContext = false)]获取或设置一个值,该值指定是否使用当前同步上下文来选择执行的线程。为false则使用新线程执行Callback中的方法
- Session:
- basicHttpBinding不支持Session
- [ServiceContract(SessionMode = SessionMode.NotAllowed)]可以设置不支持session.
- [OperationContract(IsInitiating = false,IsTerminating =true)],[OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)],设置开始咳结束session的operation.
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]指定InstanceContextMode为PerCall,每次调用service都会初始化一个Instance,InstanceContextMode = InstanceContextMode.PerSession,允许session,Instance,InstanceContextMode = InstanceContextMode.Single,,只初始化一个instance,所有调用的状态都将保留
- 在Client端捕获异常,设置 <serviceDebug includeExceptionDetailInFaults="true" />,但是只能在Debug模式下使用,在Release下会暴漏一些敏感信息.
-
View Code
<behaviors> <serviceBehaviors> <behavior name="calculatorServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> -
基于Fault Contract 的Exception Handling
[OperationContract] [FaultContract(typeof(MathError))] double Divide(double x, double y);MathError是定义的用来传输异常的类,通过FaultContract指定.在Service就可以跑出这种类型的错误信息了.
- WCF 协议:


- MSMQ天生就具有异步的特性,它只能以One-way的MEP(Message Exchange Pattern)进行通信.
- OperationContext 提供对服务方法的上下文的访问权.可以再client和service端传递信息
- OperationContext.Current.OutgoingMessageHeaders添加传出消息头
- OperationContext.Current.IncomingMessageHeaders获取传入消息头
- ICallContextInitializer.该结构定义了两个方法:BeforeInvoke和AfterInvoke,允许你在真正的service方法执行前和执行后对CallContext进行初始化和清理。如何你希望在BeforeInvoke创建的对象能够被AfterInvoke,你可以将该对象作为BeforeInvoke 的返回值,在执行AfterInvoke的时候,该值将作为其中的参数。
-
创建 system.serviceModel/behaviors 的配置节处理程序时出错: 无法将扩展元素“CultureSettingElement”添加到此元素中。请验证该扩展是否已在 system.serviceModel/extensions/behaviorExtensions 中的扩展集合中注册。
参数名: element .网上已经有说明type中每个元素都是必须的,而且在每个逗号后边要有空格,强调一下,多一个空格也是会出现这个错误的...伤不起
View Code
<behaviorExtensions> <add name="CultureSettingElement" type="Messages.CallContextInitializers.CultureSettingBehaviorElement, Messages.CallContextInitializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </behaviorExtensions> - 逻辑地址与物理地址:Address定义的是逻辑地址,ViaUri和ListenUri定义的是物理地址.
- 基于TCP端口共享的编程:当你第一次使用Net.TCP Port Sharing Service,或者发现该服务被禁用,你需要手工的启用该服务。 Net.TCP Port Sharing Service
-
NetTcpBinding binding = new NetTcpBinding();
binding.PortSharingEnabled = true;
或者<bindings>
<netTcpBinding>
<binding name="portSharingBinding" portSharingEnabled="true" />
</netTcpBinding></bindings>
View Code
浙公网安备 33010602011771号