合同类型未归因于 ServiceContractAttribute

Contract Type is Not Attributed with 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);

}

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

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

您的 ServiceContract 属性似乎不在您的具体 class 中,试试这个。

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