WCF - use without app.config

2020-03-07 08:03发布

问题:

I have a SharePoint workflow which calls a WCF service. This works fine so long as the workflow runs under IIS and is not shifted to the Timer Service.

The problem is that the Timer Service does not have access to the web.config settings it needs to setup the WCF connection from the context of the Timer Service.

Could not find endpoint element with name endpointname' and contract 'servicecontractname' in the ServiceModel client configuration section

I am setting up all the info WCF needs to make the connection in code anyway (and overriding the values set in web.config)

My question is, can I bypass this config entirely? I'd rather not be dependent on several ettings files and keeping them in sync.

Update This little bit of code did the trick.

string address = "http://myservice.com/soap.svc";
Binding binding = new System.ServiceModel.BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress(address);
client = new MyServiceClient(binding, endpointAddress);

Thanks for the input!

回答1:

Sure - you can do all configuration in code.

Uri tcpBaseAddress = new Uri("net.tcp://localhost:8000/");

ServiceHost host = new ServiceHost(typeof(MyService),tcpBaseAddress);

Binding tcpBinding = new NetTcpBinding( );

//Use base address as address
host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,"");
//Add relative address
host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,"MyService");
//Ignore base address
host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,
   "net.tcp://localhost:8001/MyService");

host.Open( );

http://en.csharp-online.net/WCF_Essentials%E2%80%94Programmatic_Endpoint_Configuration



回答2:

Here is one that comes straight from our SharePoint code for our PDF Converter product. It uses HTTPBindings and completely bypasses config files.

    /// <summary>
    /// Configure the Bindings, endpoints and open the service using the specified address.
    /// </summary>
    /// <returns>An instance of the Web Service.</returns>
    public static DocumentConverterServiceClient OpenService(string address)
    {
        DocumentConverterServiceClient client = null;

        try
        {
            BasicHttpBinding binding = new BasicHttpBinding();
            // ** Use standard Windows Security.
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ClientCredentialType = 
                                                        HttpClientCredentialType.Windows;
            // ** Increase the Timeout to deal with (very) long running requests.
            binding.SendTimeout = TimeSpan.FromMinutes(30);
            binding.ReceiveTimeout = TimeSpan.FromMinutes(30);
            // ** Set the maximum document size to 40MB
            binding.MaxReceivedMessageSize = 50*1024*1024;
            binding.ReaderQuotas.MaxArrayLength = 50 * 1024 * 1024;
            binding.ReaderQuotas.MaxStringContentLength = 50 * 1024 * 1024;

            // ** Specify an identity (any identity) in order to get it past .net3.5 sp1
            EndpointIdentity epi = EndpointIdentity.CreateUpnIdentity("unknown");
            EndpointAddress epa = new EndpointAddress(new Uri(address), epi);

            client = new DocumentConverterServiceClient(binding, epa);

            client.Open();

            return client;
        }
        catch (Exception)
        {
            CloseService(client);
            throw;
        }
    }