2013-10-17 21 views
0

我在2小時內尋找解決方案,因此我放棄並在此處張貼。在SQL中調用WCF服務器的DLL錯誤:找不到引用合同的默認端點元素

我有一個C#3.5 DLL創建。它的目標是soooooo方便:

public static string CallWsMethodClient(string sXMLSettings, string sXMLIn) 
    { 
     try 
     { 
      WS_Generic.ServiceClient serv = new WS_Generic.ServiceClient(); 
      return serv.CallWsMethod(sXMLSettings, sXMLIn); 
     } 
     catch (Exception e) 
     { 
      XElement xRootNode = new XElement("ALL_XML_OUT"); 
      xRootNode.Add(new XElement("DLL_ERROR_MESS", e.GetType().Name + " - " + e.Message)); 
      xRootNode.Add(new XElement("DLL_ERROR_STACKTRACE", e.StackTrace)); 
      xRootNode.Add(new XElement("DLL_ERROR_INNER", e.InnerException)); 
      return xRootNode.ToString(); 
     } 
    } 

我有一個服務引用的web服務(WS_Generic.ServiceClient)。

我的目標是在SQL Server 2008R2中彙編這個dll作爲程序集,並調用SQL中的方法來調用websevice。

我導入DLL與該命令:

create assembly [blabla] 

從 'XXXX \ blabla.dll' 與PERMISSION_SET =不安全

創建與庫存過程:

create function CallWsMethodClient(@sXMLSettings nvarchar(max), @sXMLIn nvarchar(max)) 
     returns nvarchar(max) external name blabla.[WCF_SQL.WcfClient].CallWsMethodClient 

當我執行我的庫存程序... TADA !!!!!

<ALL_XML_OUT> 
    <DLL_ERROR_MESS>InvalidOperationException - Could not find default endpoint element that references contract 'WS_Generic.IService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.</DLL_ERROR_MESS> 
    <DLL_ERROR_STACKTRACE> at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName) 
    at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address) 
    at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress) 
    at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName) 
    at System.ServiceModel.EndpointTrait`1.CreateSimplexFactory() 
    at System.ServiceModel.EndpointTrait`1.CreateChannelFactory() 
    at System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait) 
    at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef() 
    at System.ServiceModel.ClientBase`1..ctor() 
    at WCF_SQL.WS_Generic.ServiceClient..ctor() 
    at WCF_SQL.WcfClient.CallWsMethodClient(String sXMLSettings, String sXMLIn)</DLL_ERROR_STACKTRACE> 
    <DLL_ERROR_INNER /> 
</ALL_XML_OUT> 

我只想死......任何人都有想法?

當然,我的dll的配置文件是name_of_dll.dll.config。

可能dll在內存中,所以我必須在dll中編寫我的端點?問題是我必須重新編譯它,每次web服務的URL改變。

在此先感謝,

+1

「我的DLL的配置文件是name_of_dll.dll.config」 - 你一定要明白,DLL的不要」使用配置文件,對不對?除非你在DLL中編寫了一些自定義的東西。 – Tim

+0

當你添加一個服務引用到你的dll時,Visual Studio創建一個dll的配置文件來配置端點。它的名字是VS中的app.config,編譯後是name_of_the_dll.dll.config。 – BaptX

+0

是的。並且,當您在Visual Studio中創建類庫(DLL)項目時,它也包含一個app.config文件 - 並不意味着該DLL實際使用它。我沒有在SQL Server中運行託管代碼,所以它可能在這種情況下會有所不同,但我廣泛使用DLL和WCF,並且我一直必須將服務模型部分放入消費應用程序的配置文件中。至少按我的經驗,DLL總是使用調用應用程序的配置文件。 – Tim

回答

0

好吧,爲了解決這個問題,我遵循了Ben Robinson的方式。

這個DLL的目標永遠不會改變,或者只有幾次。

所以我把綁定代碼:

 [...]  

     serv = new WS_Generic.ServiceClient(ConfigureBinding(), ConfigureEndPointAddress()); 

     [...] 


     private static EndpointAddress ConfigureEndPointAddress() 
     { 
      EndpointAddress endpointAddress = new EndpointAddress("xxx"); 

      return endpointAddress; 
     } 

     private static BasicHttpBinding ConfigureBinding() 
     { 
      BasicHttpBinding binding = new BasicHttpBinding(); 

      binding.AllowCookies = false; 
      binding.BypassProxyOnLocal = false; 
      binding.CloseTimeout = TimeSpan.Parse("00:01:00"); 
      binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; 
      binding.MaxBufferPoolSize = 524288; 
      binding.MaxBufferSize = 5242880; 
      binding.MaxReceivedMessageSize = 5242880; 
      binding.MessageEncoding = WSMessageEncoding.Text; 
      binding.Name = "BasicHttpBinding_IService"; 
      binding.OpenTimeout = TimeSpan.Parse("00:01:00"); 
      binding.ReceiveTimeout = TimeSpan.Parse("00:10:00"); 
      binding.SendTimeout = TimeSpan.Parse("00:01:00"); ; 
      binding.TextEncoding = Encoding.UTF8; 
      binding.TransferMode = TransferMode.Buffered; 
      binding.UseDefaultWebProxy = true; 

      return binding; 
     } 

和它的作品吧!只有一個問題仍然存在:當我想改變目標時,我必須重新編譯dll!

感謝您的建議:)

+0

現在,您可以處理在Web服務調用阻止您的事務時引起的阻塞和死鎖......此外,原始答案表示將配置信息存儲在表中,而不是硬編碼 –

+0

你說得對。但是,如前所述,將配置放入數據庫沒有任何優勢:配置永遠不會改變。 – BaptX

0

您正在編寫生活在dB中的代碼。

爲什麼不傳入端點URL作爲存儲在dB中的參數。

+0

這不是一個愚蠢的想法,但目標是找到爲什麼我的配置文件就像一個幽靈:)但謝謝你的想法。問題是保存端點的所有參數-_- – BaptX

+0

.net clr DLL永遠不會在配置文件中拾取任何東西。 –

+0

是的,但在開始時很難理解:) – BaptX

相關問題