简体   繁体   English

在 SQL 中创建一个调用 WCF 服务器的 DLL 错误:找不到引用合同的默认端点元素

[英]Making a DLL in SQL which call WCF Server Error : Could not find default endpoint element that references contract

I'm searching for a solution from 2 hours, so I give up and post here.我正在寻找 2 个小时的解决方案,所以我放弃并在这里发布。

I have a C# 3.5 DLL created.我创建了一个 C# 3.5 DLL。 It's goal it soooooo easy :它的目标太简单了:

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();
        }
    }

I have a service reference to a webservice (WS_Generic.ServiceClient).我有一个对网络服务 (WS_Generic.ServiceClient) 的服务引用。

My objective is to import this dll as assembly in SQL Server 2008R2, and call the method from SQL in order to call the web sevice.我的目标是在 SQL Server 2008R2 中将此 dll 作为程序集导入,并从 SQL 调用该方法以调用 Web 服务。

I import the DLL with this command :我使用以下命令导入 DLL:

create assembly [blabla]

from 'xxxx\\blabla.dll' with permission_set = unsafe来自 'xxxx\\blabla.dll' with permission_set = unsafe

I create the stored procedure with :我使用以下方法创建存储过程:

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

And when I execute my stored procedure... TADA !当我执行我的存储过程时...... 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>

I just want to die... Anybody have an idea ?我只想死...有人有想法吗?

Of course, the config file of my dll is name_of_dll.dll.config.当然,我的dll的配置文件是name_of_dll.dll.config。

May the dll is in memory, so I have to code in the dll my endpoint ? dll可能在内存中,所以我必须在我的端点的dll中编码? Bu the problem is I have to recompile it each time the url of web service change.但问题是每次Web服务的url更改时我都必须重新编译它。

Thanks in advance提前致谢

You are writing code that lives in a dB.您正在编写存在于 dB 中的代码。

Why not pass in the endpoint URL as a parameter which is stored in the dB.为什么不将端点 URL 作为存储在 dB 中的参数传入。

Ok so for the solution, I followed the way of Ben Robinson.好的,对于解决方案,我遵循了 Ben Robinson 的方法。

The target of this DLL will never change, or only few times.这个 DLL 的目标永远不会改变,或者只会改变几次。

So I put bindings in code :所以我把绑定放在代码中:

        [...]    

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

And it works now !它现在起作用了! Only one problem remains : I have to recompile the dll when I want to change the target !只剩下一个问题:当我想改变目标时,我必须重新编译 dll!

Thanks for all your advice :)感谢您的所有建议:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM