WCF-不带app.config使用 [英] WCF - use without app.config

查看:166
本文介绍了WCF-不带app.config使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用WCF服务的SharePoint工作流。只要工作流在IIS下运行并且不转移到Timer Service,它就可以正常工作。



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



在ServiceModel客户端配置部分中找不到名称为endpointname'和合同为'servicecontractname'的终结点元素



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



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



更新
这一点代码确实

 字符串地址= http://myservice.com/soap.svc; 
绑定绑定=新的System.ServiceModel.BasicHttpBinding();
EndpointAddress endpointAddress =新的EndpointAddress(地址);
client = new MyServiceClient(binding,endpointAddress);

感谢您的输入!

解决方案

确定-您可以在代码中进行所有配置。

  Uri tcpBaseAddress = new Uri( net.tcp:// localhost:8000 /); 

ServiceHost主机=新的ServiceHost(typeof(MyService),tcpBaseAddress);

绑定tcpBinding = new NetTcpBinding();

//将基地址用作地址
host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,);
//添加相对地址
host.AddServiceEndpoint(typeof(IMyContract),tcpBinding, MyService);
//忽略基址
host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,
net.tcp:// localhost:8001 / MyService);

host.Open();

http://en.csharp-online.net/WCF_Essentials%E2%80%94Programmatic_Endpoint_Configuration


I have a SharePoint workflow which calls a WCF service. This works fine so long as the workflow runs under IIS and is not shifted to the Timer Service.

The problem is that the Timer Service does not have access to the web.config settings it needs to setup the WCF connection from the context of the Timer Service.

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

I am setting up all the info WCF needs to make the connection in code anyway (and overriding the values set in web.config)

My question is, can I bypass this config entirely? I'd rather not be dependent on several ettings files and keeping them in sync.

Update This little bit of code did the trick.

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

Thanks for the input!

解决方案

Sure - you can do all configuration in code.

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

这篇关于WCF-不带app.config使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆