c# - 如何使用反射和app.config调用wcf?

标签 c# wcf reflection app-config

我在vs2010中创建了一个类库项目[MyLibrary]并添加了Service Reference[http://127.0.0.1/MyService.svc]。所以它在app.config中包含了这样的节点。

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IMyService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://127.0.0.1/MyService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
            contract="MyService.IMyService" name="BasicHttpBinding_IMyService" />
    </client>
</system.serviceModel>

我编译 MyLibrary 项目,它生成 MyLibrary.dll 和 MyLibrary.dll.config。 通常,我可以调用 wcf 方法,例如:

MyService.MyServiceClient client = new MyServiceClient(); 

int result = client.Add(3,6);

我没有通过programe操作过app.config。它运行良好。

现在,我编写了另一个程序来加载 MyLibrary.dll 并使用 refelection 调用 wcf 方法。它生成错误: 在 ServiceModel 客户端配置部分找不到引用契约(Contract)“MyService.IMyService”的默认终结点元素。这可能是因为没有找到您的应用程序的配置文件,或者因为在客户端元素中找不到匹配此协定的端点元素。

我认为它没有在运行时使用反射读取app.config中的配置。我尝试使用这种方法,它仍然不起作用。

string assemblyPath = Assembly.GetExecutingAssembly().Location;
string configPath = assemblyPath + ".config";
currentDomain.SetData("APP_CONFIG_FILE", configPath);
typeof(ConfigurationManager)
    .GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static)
    .SetValue(null, 0);

typeof(ConfigurationManager)
    .GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static)
    .SetValue(null, null);

typeof(ConfigurationManager)
    .Assembly.GetTypes()
    .Where(x => x.FullName == "System.Configuration.ClientConfigPaths").First()
    .GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static)
    .SetValue(null, null);

如果我不想更改上面的调用wcf代码,我该怎么办?如何让程序在运行时使用反射加载和识别app.config。似乎无用的反射(reflection)。谢谢!

最佳答案

你应该复制<system.servicemodel>从 MyLibrary.dll.config 到应用程序的 app.config 部分引用了 MyLibrary.dll。应该够了。不管怎样,你问的是如何加载 app.config。在这个post描述了如何从任何文件加载 WCF 客户端配置。但同样,复制服务模型部分应该就足够了。

关于c# - 如何使用反射和app.config调用wcf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15153329/

相关文章:

c# - 在 XAML 中设置时访问代码中的 DataContext 实例

.net - 如何在更改 WCF 服务应用程序项目中的命名空间时运行 WCF 服务?

c# - 异常 : Maximum number of items that can be serialized or deserialized in an object graph is '65536'

inheritance - 如何在 Golang 中为多种类型编写一个函数?

c# - 如何在运行时更改静态方法的行为?

java - 方面与实例方法中的类 getName() 行为

c# - 如何在 c# neo4jClient 中创建一个唯一节点(如果已经存在则不重复)?

c# - 扩展 GroupPrincipal 和 Members 属性

c# - WCF/客户端应用程序——业务逻辑应该放在哪里?

javascript - 值更改时触发服务