调用简单服务失败并显示明显错误的错误消息

Call to simple service fails with an apparently erroneous error message

当我调用这个服务时,我收到了这个错误信息:

ContractDescription has zero operations; a contract must have at least one operation

这毫无意义,因为我的界面函数定义了 [OperationContract()] 属性。

界面:

[ServiceContract()]
public interface ITest
{

    [OperationContract()]
    bool Connect(string password);

}

SVC:

<%@ ServiceHost Language="CS" Debug="true" Service="TestService.Test" CodeBehind="Test.svc.cs" %>

svc.cs:

public class Test : ITest
{

public bool Connect(string password)
{
    return true;
}

}

调用: 配置以编程方式定义,因为它是一个库

public sealed class Validator
{

    public static bool Connect(string password)
    {
        return ObtenirCLient().Connect(password);
    }

    private static LicensingService.LLMrqLicensingClient ObtenirCLient()
    {
        dynamic endpoint = new EndpointAddress("http://localhost/TestService/Test.svc");
        LicensingService.LLMrqLicensingClient client = new LicensingService.LLMrqLicensingClient(ObtenirBinding(), endpoint);

        client.Endpoint.Name = "LicHttp";
        client.Endpoint.Contract = new Description.ContractDescription("TestService.ITest");

        return client;
    }

    private static BasicHttpBinding ObtenirBinding()
    {
        return new BasicHttpBinding {
            Name = "LicHttp",
            Security = ObtenirSecurity()
        };
    }

    private static BasicHttpSecurity ObtenirSecurity()
    {
        return new BasicHttpSecurity {
            Mode = BasicHttpSecurityMode.TransportCredentialOnly,
            Transport = ObtenirTransport()
        };
    }

    private static HttpTransportSecurity ObtenirTransport()
    {
        return new HttpTransportSecurity { ClientCredentialType = HttpClientCredentialType.Windows };
    }

}

如果你看到任何奇怪的东西,请告诉我!

而不是

client.Endpoint.Contract = new Description.ContractDescription("TestService.ITest");

试试这个:

client.Endpoint.Contract = ContractDescription.GetContract(typeof(ITest));