WCF - 在没有app.config的情况下使用

时间:2011-02-28 17:32:34

标签: wcf sharepoint sharepoint-2007 config

我有一个调用WCF服务的SharePoint工作流。只要工作流在IIS下运行并且没有转移到定时服务,这样就可以正常工作。

问题是Timer Service无法访问从Timer Service上下文设置WCF连接所需的web.config设置。

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

我正在设置WCF所需的所有信息,无论如何都要在代码中建立连接(并覆盖web.config中设置的值)

我的问题是,我可以完全绕过此配置吗?我宁愿不依赖于几个ettings文件并保持同步。

更新 这一小段代码就行了。

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

感谢您的投入!

2 个答案:

答案 0 :(得分:3)

当然 - 您可以在代码中进行所有配置。

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

答案 1 :(得分:1)

这是一个直接来自我们的SharePoint code for our PDF Converter product。它使用HTTPBindings并完全绕过配置文件。

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