2012-07-19 38 views
3

我已经创建了一个类库,该类库应该连接到我托管的WCF端点项目。 客户端项目已定义了应与该服务交互的命令行开关。无法找到引用合同的默认端点元素:通过Powershell cmdlet连接到WCF端点

不过,我不断收到以下错误:

 Could not find default endpoint element that references contract Service1.MyService 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. 

你知道这个问题可能是什么?

编辑 我有一个定义cmdlet的类库。我使用.psd1文件导入模块,它使用生成的dll文件。

EDIT2 再一次,我没有一个项目引用我的图书馆。这是调用命令行开关定义,这些cmdlet应该连接到WCF终结

感谢

+0

老帖子,但我发现一个观察到这个错误了。如果您有2个程序集,其中一个程序集在另一个具有服务引用的程序集中调用方法,而另一个程序集是启动程序集,则您的服务配置也需要位于启动程序集中。 – Eon 2015-11-30 10:39:38

回答

9

已经得到这个工作:这里是一个干净的解决方案:

internal static class ServiceClass 
{ 

    internal static Object GetWCFSvc(string siteUrl) 
    { 

     Uri serviceUri = new Uri(siteUrl); 
     EndpointAddress endpointAddress = new EndpointAddress(serviceUri); 

     //Create the binding here 
     Binding binding = BindingFactory.CreateInstance(); 

     ServiceClient client = new ServiceClient(binding, endpointAddress);    
     return client; 
    } 


} 

internal static class BindingFactory 
{ 
    internal static Binding CreateInstance() 
    { 
     BasicHttpBinding binding = new BasicHttpBinding(); 
     binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
     binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; 
     binding.UseDefaultWebProxy = true; 
     return binding; 
    } 

} 
+0

感谢您的回答!你救了我的一天.. – karthik 2013-06-20 05:34:13

+0

谢谢,真的帮了我很多 – Jeppen 2014-11-21 16:23:20

+0

非常感谢我们! – johnf 2014-11-25 15:15:32

1

创建与启动项目

+0

你是什么意思? – 2012-07-19 21:55:39

+0

什么是启动项目。我所拥有的只是一个定义cmdlet并且是类库的项目。我使用psd1文件为cmdlet导入模块 – 2012-07-19 21:56:43

+1

他意味着使用该库的项目应该包含一个包含该端点的配置文件。不是图书馆本身。 – Silvermind 2012-07-19 21:57:26

0

有点搜索后端点配置文件中的PowerShell中,可能会发现你的答案 existing link

希望这有助于

+0

我看着这个,我看不出它是如何适用于此。见编辑 – 2012-07-19 21:57:01

0

当你运行你的cmdlet的汇编代码,执行过程是powershell.exe(在%WINDIR%\ system32 \ WindowsPowershell \ v1.0或%windir%\ syswow64 \ WindowsPowershell \ v1.0),所以任何System.ServiceModel配置都需要在powershell配置文件(powershell.exe.config)中定义。

我猜这可能不是一个实际的选择,所以你可能需要手动配置你的服务客户端或通道工厂,而不是通过应用程序配置文件。

在这里看到一个例子:http://msdn.microsoft.com/en-us/library/ms734681.aspx

另一个选择可能是使用激活的配置文件:http://msdn.microsoft.com/en-us/library/ff361644.aspx

我不知道有多好,这将服务配置元素的工作。

+0

我会研究一下。除了更改powershell exe配置之外,这是唯一的方法吗? – 2012-07-20 02:56:42

+0

据我所知,没有办法从应用程序配置文件以外的任何地方加载配置节,在你的情况下,应用程序是powershell。 – lesscode 2012-07-20 03:04:10

+0

查看更多的答案为另一种可能的选项 – lesscode 2012-08-08 18:09:18

相关问题