随笔 - 90  文章 - 0 评论 - 94 阅读 - 60万

如果先关service再关client

那么client的channel.Close()会报异常:

“The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.”

 

如果还未启动service就打开client

则client的channel.Open()会报异常:

“The endpoint was not found. Please ensure that you can connect to the internet using HTTP port 80 and TCP port 808.”

 

Client与Service两边的属性的Namespace不一致

例如:

Client端:

    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    
public interface IEchoContract
    {
        [OperationContract]
        String Echo(
string text);
    }


Server端:

复制代码
     // 注意,最后多了一个Echo
    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/Echo")]
    
public interface IEchoContract
    {
        [OperationContract]
        String Echo(
string text);
    }
复制代码

在Client执行方法channel.Echo(input)的时候会报异常:

"The message with Action 'http://samples.microsoft.com/ServiceModel/Relay/IEchoContract/Echo' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None)."

 

避免Contract和Behavior的paste&copy error

如果一个该用ServiceBehavior的地方用了ServiceContract,例如:

 

复制代码
//should be [ServiceBehavior(Name = "EchoService", Namespace = http://samples.microsoft.com/ServiceModel/Relay/)]
[ServiceContract(Name = "IEchoContract", Namespace = http://samples.microsoft.com/ServiceModel/Relay/)]
public class EchoService : IEchoContract
{
    
public string Echo(string text)
    {
        Console.WriteLine(
"Echoing: {0}", text);
        
return text;
    }
}
复制代码

则在new一个host的时候,ServiceHost host = new ServiceHost(typeof(EchoService), address); // 会抛出异常
异常信息:

"The service class of type Microsoft.ServiceBus.Samples.EchoService both defines a ServiceContract and inherits a ServiceContract from type Microsoft.ServiceBus.Samples.IEchoContract. Contract inheritance can only be used among interface types.  If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute.  Consider moving the ServiceContractAttribute on type Microsoft.ServiceBus.Samples.IEchoContract to a separate interface that type Microsoft.ServiceBus.Samples.IEchoContract implements."

posted on   MainTao  阅读(3061)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示