Как установить пространство имен контрактов во время выполнения?

#c# #wcf

#c# #wcf

Вопрос:

У меня есть следующее в файле client 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:

Вы не связываете контракт с EndpointAddress , вы связываете его с ServiceEndpoint . И, в свою очередь, вы также связываете свое EndpointAddress с приведенным ServiceEndpoint ниже:

 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);