如何在带有配置文件的 Powershell 脚本中使用自定义 WCF 代理? [英] How to use a custom WCF proxy in a Powershell script with a config file?

查看:46
本文介绍了如何在带有配置文件的 Powershell 脚本中使用自定义 WCF 代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自己的程序集中有一个手写的 WCF 代理,非常简单:

I have a hand-written WCF proxy in it's own assembly, it's very simple:

public class MyServiceClient : ClientBase<IMyService>, IMyService
{
    public MyServiceClient()
    {
    }

    public MyServiceClient(string endpointConfigurationName) :
        base(endpointConfigurationName)
    {
    }
}

我正在将其加载到 Powershell 脚本中:

I am loading this into a Powershell script:

Add-Type -Path "$LocalPath\MyService.Client.dll"
Add-Type -Path "$LocalPath\MyService.Contracts.dll"

然后我尝试设置 App.config(根据 SO 上的其他帖子),以便可以使用配置中定义的端点实例化客户端,而不是在脚本本身中:

I am then trying to set the App.config (as per other posts on SO) so that the client can be instantiated with an Endpoint defined in config, rather than in the script itself:

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$LocalPath\MyService.Client.dll.config")

我已经检查了 AppDomain 并将配置文件设置为其 ConfigurationFile 属性.

I have checked the AppDomain and the config file is set as its ConfigurationFile property.

当我创建客户端实例时:

When I create an instance of the client:

$endpointName = "MyServiceHttpEndpoint" # defined in the app.config file
$myclient = New-Object MyService.Client.MyServiceClient($endpointName)

它倒下了说:

使用1"参数调用.ctor"的异常:在 ServiceModel 客户端配置部分中找不到名称为MyServiceHttpEndpoint"和合同为MyService.Contracts.IMyService"的端点元素.这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素."

有什么想法吗?我不想在脚本文件中手动创建端点 - 它需要从配置中读取.

Any ideas? I don't want to manually create the endpoint in the script file - it needs to be read from config.

推荐答案

Powershell 和 Powershell ISE 处理此问题的方式似乎有所不同.

There seems to be a difference in the way that Powershell and Powershell ISE handle this.

使用 ISE(至少是我使用的版本),您必须清除配置以强制重新加载.不过,我想您也可以将 .dll.config 文件的内容放入 powershell ISE 配置中.然而,这似乎很恶心.下面发布的代码有效.我在谷歌上发现了这个问题的部分内容.

With ISE (at least the version I'm using) you have to clear out the configuration to force it to reload. Though, I guess you could also put the contents of your .dll.config file into the powershell ISE config. However, that seems nasty. The code posted below works. I found parts of it googling this issue.

# $dllPath is the path to the dll we want to load
# first point to the correct config file
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$dllPath.config")

# PowerShell ISE is a PITA we have to override the config
if ($psISE -ne $null) {
    Add-Type -AssemblyName System.Configuration
    [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null,0)
    [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null,$null)
    ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)
}

#Now load the DLL
$null = [Reflection.Assembly]::LoadFrom($dllPath)

# DLL and Config should be loaded - test

这篇关于如何在带有配置文件的 Powershell 脚本中使用自定义 WCF 代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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