Valamas
Valamas

Reputation: 24759

How to set contract namespace at runtime?

I have the following in the 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>

I have the following code which sets up an endpoint during runtime.

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

How can I set the contract value at runtime?

thank you

Upvotes: 0

Views: 701

Answers (1)

Kirk Woll
Kirk Woll

Reputation: 77606

You don't associate the contract with the EndpointAddress, you associate it with the ServiceEndpoint. And in turn, you also associate your EndpointAddress with the ServiceEndpoint as below:

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

Ultimately, it is this instance of ServiceEndpoint that should be added to the ServiceHost:

host.AddServiceEndpoint(httpEndpoint);

Upvotes: 1

Related Questions