2013-03-01 22 views
2

我在vs2010中创建了一个类库项目[MyLibrary]并添加了Service Reference [http://127.0.0.1/MyService.svc]。因此它在app.config中包含了这样的节点。如何使用反射和app.config调用wcf?

<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.it运行app.config,效果很好。

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

我认为它已经不能读取使用在runtime.I反射尝试使用这种方法的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。似乎无用的反思。谢谢!

+0

在你的“新程序”中,你的app.config中有什么? – 2013-03-01 08:28:37

+0

Nothing,MyLibrary refenrence wcf,因此wcf客户端配置存在于Mylibrary.dll.config.At运行时,当调用wcf方法时,如何自动找到Mylibrary.dll.config中的wcf配置。我仍然使用这样的代码:MyService.MyServiceClient client = new MyServiceClient(); – gmplayer 2013-03-01 08:34:26

+0

如果我没有改变调用的方式,这样的错误:找不到在ServiceModel客户端配置部分中引用合同'MyService.IMyService'的默认端点元素。因此,我认为它不能在Mylibrary中获得wcf配置.dll.config使用反射。 – gmplayer 2013-03-01 08:37:05

回答

0

您应该MyLibrary.dll.config复制<system.servicemodel>部分的app.config您的应用程序引用MyLibrary.dll的。这应该够了。无论如何,你问的是如何加载app.config。在此post中描述了如何从任何文件加载WCF客户端配置。但是,再次,它应该足以复制servicemodel部分。

+0

谢谢,我无法将MyLibrary.dll.config中的部分复制到app.config.I无法运行app.config,MyLibrary.dll由Quarz.net调用。 – gmplayer 2013-03-01 08:58:36

0

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

MyService.MyServiceClient client = new MyServiceClient(); 

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