2011-02-28 46 views
1

我有一個調用WCF服務的SharePoint工作流。只要工作流程在IIS下運行並且不會轉移到定時服務,此工作正常。WCF - 使用不帶app.config

問題是,定時服務無法訪問它需要從定時服務上下文建立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); 

感謝您的輸入!

+0

我貼我的答案是使用此處WsHttpBinding的 http://stackoverflow.com/questions/746107/c-wcf-having-a-single-app-config-in-a-shared-庫 - 即 - 提供存取到個/ 7652518#7652518 – 2011-10-04 20:04:14

回答

2

當然 - 您可以在代碼中進行所有配置。

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

這是一個從我們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; 
     } 
    } 
相關問題