WCF - 无需 app.config 即可使用

2023-12-12

我有一个调用 WCF 服务的 SharePoint 工作流。只要工作流在 IIS 下运行并且不转移到计时器服务,这种方法就可以正常工作。

问题是计时器服务无法访问从计时器服务上下文设置 WCF 连接所需的 web.config 设置。

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

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

我的问题是,我可以完全绕过这个配置吗?我不想依赖多个设置文件并使它们保持同步。

Update这段代码就达到了目的。

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

感谢您的投入!


当然 - 您可以在代码中完成所有配置。

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%94 编程端点_配置

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WCF - 无需 app.config 即可使用 的相关文章

随机推荐