2

我在 vs2010 中创建了一个类库项目[MyLibrary] 并添加了服务参考[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 结果 = client.Add(3,6);

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

现在,我编写了另一个程序来加载 MyLibrary.dll 并使用 refelection 调用 wcf 方法。它会生成错误:在 ServiceModel 客户端配置部分中找不到引用合同“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。似乎无用的反射。谢谢!

4

2 回答 2

0

您应该将部分从 MyLibrary.dll.config 复制<system.servicemodel>到应用程序引用 MyLibrary.dll 的 app.config。应该够了。无论如何,您是在问如何加载 app.config。在这篇文章中描述了如何从任何文件加载 WCF 客户端配置。但同样,复制 servicemodel 部分就足够了。

于 2013-03-01T08:52:57.183 回答
0

它与通过反射调用无关。有错误消息表明Mylibrary.dll.config找不到配置。如果您在没有将配置添加到新程序的 app.config(或 web.config)的情况下直接调用引用 Mylibrary.dll 的客户端代码,则会收到相同的错误。

MyService.MyServiceClient client = new MyServiceClient(); 

默认情况下,上面的代码会在当前运行进程的配置文件中查找<system.servicemodel>. 您的新程序的配置需要将 Mylibrary.dll.config 文件中的信息添加到它的配置文件中。否则,您必须直接在代码中配置客户端。

于 2013-03-01T09:02:20.243 回答