我在它自己的程序集中有一个手写的 WCF 代理,它非常简单:
public class MyServiceClient : ClientBase<IMyService>, IMyService
{
public MyServiceClient()
{
}
public MyServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
}
我将其加载到 Powershell 脚本中:
Add-Type -Path "$LocalPath\MyService.Client.dll"
Add-Type -Path "$LocalPath\MyService.Contracts.dll"
然后我尝试设置 App.config (按照其他帖子 https://stackoverflow.com/questions/835862/powershell-calling-net-assembly-that-uses-app-configon SO),以便可以使用配置中定义的端点实例化客户端,而不是在脚本本身中:
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$LocalPath\MyService.Client.dll.config")
我已经检查了 AppDomain 并将配置文件设置为其ConfigurationFile
财产。
当我创建客户端实例时:
$endpointName = "MyServiceHttpEndpoint" # defined in the app.config file
$myclient = New-Object MyService.Client.MyServiceClient($endpointName)
它倒下说:
Exception calling ".ctor" with "1" argument(s): "Could not find endpoint element with name 'MyServiceHttpEndpoint' and contract 'MyService.Contracts.IMyService' 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 name could be found in the client element."
有任何想法吗?我不想在脚本文件中手动创建端点 - 它需要从配置中读取。