合同类型不归因于ServiceContractAttribute

尝试创建使用动态端口而不是硬编码端口值的应用程序时,我遇到了以下错误:

System.InvalidOperationException: The contract type MLITS.Pulse.Pulse is not attributed with ServiceContractAttribute. In order to define a valid contract, the specified type (either contract interface or service class) must be attributed with ServiceContractAttribute.

我遇到问题的代码如下:

public static void DynamicAddress()
{
    int sessionIdentification = 0;
    int portNumber = 0;
    int newPort = 0;
    string uriString = string.Empty;

    sessionIdentification = Process.GetCurrentProcess().SessionId;
    portNumber = 14613;
    newPort = portNumber + sessionIdentification;

    uriString = "net.tcp://localhost:" + newPort + "/PulseService";

    Uri uri = new Uri(uriString);

    //ServiceHost objServiceHost = new ServiceHost(typeof(Pulse), uri);

    ServiceHost objServiceHost = new ServiceHost(typeof(Pulse));
    objServiceHost.Description.Endpoints.Clear();
    objServiceHost.AddServiceEndpoint(typeof(Pulse), new NetTcpBinding(), uri);

}

注释掉的部分是我以前的内容.但是,我意识到我需要清除配置文件中设置的端点;这就是后来的代码存在的原因,也是我遇到麻烦的地方.配置文件的原因已解释为here,这是我发布的另一个问题,以帮助解决我之前遇到的问题.找到解决方案后,我将在两个问题上发布我的结果并关闭它们.

有谁知道我在做什么错,并且知道如何解决我收到的错误?

解决方法:

看来您的ServiceContract属性不在您的具体类中,请尝试此操作.

ServiceHost objServiceHost = new ServiceHost(typeof(Pulse));
objServiceHost.Description.Endpoints.Clear();
objServiceHost.AddServiceEndpoint(typeof(IPulse), new NetTcpBinding(), uri);
上一篇:WCF中的ServiceHost初始化两种方式


下一篇:c# – 使用DI和InstanceContextMode.Percall定制ServiceHost