如何在运行时设置合约命名空间?

时间:2011-10-27 23:50:30

标签: c# wcf

我在客户端app.config中有以下内容。

<client>
    <endpoint address=""
        binding="basicHttpBinding" bindingConfiguration="SecureBinding"
        contract="Project.Services.Contract.IMyContract" name="Endpoint_Default">
        <identity>
            <servicePrincipalName value="host/mikev-ws" />
        </identity>
    </endpoint>
</client>

我有以下代码在运行时设置端点。

private EndpointAddress GetEndpoint(string serverAddress, string serviceName)
{
    string endpointURL = string.Format("http://{0}/Services/{1}.svc"
        , serverAddress, serviceName);
    EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
    Uri uri = new Uri(endpointURL);
    EndpointAddress endpoint = new EndpointAddress(uri, spn);
    return endpoint;
}

如何在运行时设置合约价值?

谢谢

1 个答案:

答案 0 :(得分:1)

您不会将合同与EndpointAddress相关联,而是将其与ServiceEndpoint相关联。反过来,您还会将EndpointAddressServiceEndpoint关联,如下所示:

ServiceEndpoint httpEndpoint = new ServiceEndpoint 
(
    ContractDescription.GetContract(
        typeof(Project.Services.Contract.IMyContract), 
        typeof(Project.Services.MyContract)),
    new WSHttpBinding { ... },
    GetEndpoint(serverAddress, serviceName)
);

最终,ServiceEndpoint的这个实例应添加到ServiceHost

host.AddServiceEndpoint(httpEndpoint);