T. J. Fan
T. J. Fan

Reputation: 77

WCF Dynamic Binding - How to specify end points?

I'm trying to bind my WCF client using code instead of app.config as I'll need to change host IP addresses for different deployment.

This is my app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
    </startup>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ICX" maxReceivedMessageSize="1073741824" >
                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://127.0.0.1/CX/CX.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICX" contract="CXService.ICX" name="WSHttpBinding_ICX">
                <identity>
                    <servicePrincipalName value="host/SilverStar" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

and this is my code:

public static void StartUp()
{
    XmlDictionaryReaderQuotas quota = new XmlDictionaryReaderQuotas();
    quota.MaxArrayLength = 2147483647;
    quota.MaxBytesPerRead = 2147483647;
    quota.MaxDepth = 2147483647;
    quota.MaxNameTableCharCount = 2147483647;
    quota.MaxStringContentLength = 2147483647;
    EndpointAddress addr = new EndpointAddress(new Uri("http://127.0.0.1/CX/CX.svc"));

    WSHttpBinding binding1 = new WSHttpBinding();
    binding1.Name = "WSHttpBinding_ICX";
    binding1.MaxReceivedMessageSize = 1073741824;
    binding1.ReaderQuotas = quota;

    // Globals.CXClient is the client object
    Globals.CXClient = new CXService.CXClient(binding1, addr);

    // This line does not compile! Endpoint is read-only!!
    Globals.CXClient.Endpoint = new ServiceEndpoint(new ContractDescription("CXService.ICX"), (Binding)binding1, addr);
}

The last line of the code does not compile as .EndPoint is read-only property.

Please help.

Upvotes: 2

Views: 642

Answers (2)

T. J. Fan
T. J. Fan

Reputation: 77

This is how I did it.

Globals.CXClient.Endpoint.Contract = new ContractDescription("CXService.ICX");
Globals.CXClient.Endpoint.Binding = binding1;
Globals.CXClient.Endpoint.Address = addr;

However, I encountered another problem. I tried to replace the part:

<identity>
    <servicePrincipalName value="host/SilverStar" />
</identity>

by using the following code:

addr.Identity = new SpnEndpointIdentity("host/SilverStar");

This one again fails to compile because addr.Identity is read-only.

I also tried this:

EndpointAddress addr = new EndpointAddress(new Uri("http://127.0.0.1/CX/CX.svc"), new SpnEndpointIdentity("host/SilverStar"), ???);

but the last parameter is an AddressHeaderCollection and I have no idea what should be placed in there.

Please help again. Thanks.

Upvotes: 0

Mohammad
Mohammad

Reputation: 2764

try this:

Globals.CXClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("your url here");

Upvotes: 1

Related Questions