C++调用C#的dll,dll的配置文件如何用

canye1984 2011-09-22 06:42:59
利用C#生成的dll用来访问soap服务器,里面有配置文件.
C++程序需要调用这个dll,但是这个配置文件如何使用?单独调用dll,访问soap服务器不成功.



C#中的配置文件app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="serviceBehavior" name="Staubli.Wcf.DebugService.DebugService">
<endpoint address="" binding="basicHttpBinding" contract="Staubli.Wcf.DebugService.IDebugService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5656" />
</baseAddresses>
<timeouts closeTimeout="00:00:01" />
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="false" httpGetUrl="" />
</behavior>
</serviceBehaviors>
</behaviors>

<bindings>
<basicHttpBinding>
<binding name="CS8ServerV0BindingConfig"
closeTimeout="00:00:10" openTimeout="00:00:10" receiveTimeout="00:10:00" sendTimeout="00:00:10"
bypassProxyOnLocal="true" useDefaultWebProxy="false"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
allowCookies="false" hostNameComparisonMode="StrongWildcard">
<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>

<customBinding>
<binding name="MtomBindingConfig">
<mtomMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" />
<httpTransport maxBufferPoolSize="52428800"
bypassProxyOnLocal="true" useDefaultWebProxy="false"
maxReceivedMessageSize="6553600" allowCookies="false" authenticationScheme="Anonymous"
keepAliveEnabled="true" maxBufferSize="6553600" proxyAuthenticationScheme="Anonymous"
transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" />
</binding>
</customBinding>
</bindings>

<client>
<endpoint contract="CS8ServerV0PortType" name="CS8ServerV0"
binding="basicHttpBinding" bindingConfiguration="CS8ServerV0BindingConfig" />

<endpoint contract="ServerV1.CS8ServerV1PortType" name="CS8ServerV1"
binding="customBinding" bindingConfiguration="MtomBindingConfig" />

<endpoint contract="CS8ServerV2PortType" name="CS8ServerV2"
binding="customBinding" bindingConfiguration="MtomBindingConfig" />

<endpoint contract="CS8ServerV3PortType" name="CS8ServerV3"
binding="customBinding" bindingConfiguration="MtomBindingConfig" />
</client>
</system.serviceModel>
</configuration>
...全文
472 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingdawin 2014-04-18
  • 打赏
  • 举报
回复
delphi调用c# dll后, 把原来的App.config文件复制到程序所在目录,然后重新改名为:现在程序的名字.exe
canye1984 2011-09-29
  • 打赏
  • 举报
回复
今天搞定了,C#生成的dll文件的配置文件,在VS中用C++编程。直接把配置文件改名放在生成的可执行文件目录中,然后把配置文件改成 可执行文件的名字+.config,其他什么都不需要做。踏破铁蹄无觅处,得来全不费功夫。还是特别感谢zzxiang1985.
canye1984 2011-09-22
  • 打赏
  • 举报
回复
非常感谢,不过测试效果好像还是不行。
纸箱猪 2011-09-22
  • 打赏
  • 举报
回复
可以参考一下这篇文章: http://navaneethkn.wordpress.com/2009/07/03/using-application-configuration-file-app-config-with-cppcli/

不过要注意打开VC项目的/clr开关,也就是使用C++/CLI。既然要在C++中调用C#库的话,想必楼主的程序也不会考虑跨平台,用C++/CLI也没关系。
canye1984 2011-09-22
  • 打赏
  • 举报
回复
在C#里面是有这个app.config的,否则也要报错,但在C++中不知道怎么使用这个配置文件。在工程里添加或者直接加都不起作用。
纸箱猪 2011-09-22
  • 打赏
  • 举报
回复
虽然我没试过在C++程序中调用C#的dll(反过来倒是试过),不过我猜测“这可能是因为未找到应用程序的配置文件”的可能性比较大。我不太清楚C#的托管程序或dll是怎样找到app.config的。
canye1984 2011-09-22
  • 打赏
  • 举报
回复
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\my file\Debug\my file.exe"
System.InvalidOperationException: 在 ServiceModel 客户端配置部分中,找不到引用协
定“CS8ServerV0PortType”的默认终结点元素
。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配
的终结点元素。
在 System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceE
ndpoint serviceEndpoint, String configurationName)
在 System.ServiceModel.ChannelFactory.ApplyConfiguration(String configuration
Name, Configuration configuration)
在 System.ServiceModel.ChannelFactory.ApplyConfiguration(String configuration
Name)
在 System.ServiceModel.ChannelFactory.InitializeEndpoint(String configuration
Name, EndpointAddress address)
在 System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationNam
e, EndpointAddress remoteAddress)
在 System.ServiceModel.EndpointTrait`1.CreateSimplexFactory()
在 System.ServiceModel.EndpointTrait`1.CreateChannelFactory()
在 System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 e
ndpointTrait)
在 System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef()
在 System.ServiceModel.ClientBase`1..ctor()
在SoapClient.SoapClient.Proxy()
纸箱猪 2011-09-22
  • 打赏
  • 举报
回复
啥异常?
canye1984 2011-09-22
  • 打赏
  • 举报
回复
成功的呀,我用C#写的dll可以访问soap服务器可以运行的。
在C++中,dll里面的函数System.ServiceModel.EndpointAddress出现异常,需要配置文件
m_address = "http://" + SoapIpaddress + ":" + SoapPort;
m_cs8ServerV0 = new CS8ServerV0PortTypeClient();
m_cs8ServerV0.Endpoint.Address = new System.ServiceModel.EndpointAddress(m_address);
这句跳出异常
纸箱猪 2011-09-22
  • 打赏
  • 举报
回复
用C#程序调用这个C#写的dll访问soap服务器成功吗?

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧