合同类型不属于 ServiceContractAttribute

Jas*_*n W 3 c# wcf servicehost

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

System.InvalidOperationException:合同类型 MLITS.Pulse.Pulse 不属于 ServiceContractAttribute。为了定义有效的契约,指定的类型(契约接口或服务类)必须使用 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);

}
Run Code Online (Sandbox Code Playgroud)

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

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

Ram*_*mie 5

看起来你的 ServiceContract 属性不在你的具体类中,试试这个。

ServiceHost objServiceHost = new ServiceHost(typeof(Pulse));
objServiceHost.Description.Endpoints.Clear();
objServiceHost.AddServiceEndpoint(typeof(IPulse), new NetTcpBinding(), uri);
Run Code Online (Sandbox Code Playgroud)