使用 spring.net 时如何从客户端命令行设置 WCF 服务的地址

发布于 2024-11-19 01:42:55 字数 1373 浏览 3 评论 0原文

我的 WCF 服务可以在任何服务器上运行。我的客户端 - 是控制台应用程序。在命令行参数中,我想设置 WCF 服务的地址。 当前在配置客户端中我有:

...
<spring>
    <context>
      <resource uri="assembly://MyAssembly.Console/MyAssembly.Console/ServerWeb.xml"/>
    </context>
  </spring>
...
<system.serviceModel>
 <client>
      <endpoint behaviorConfiguration="Default" name="serverWebDataServiceEndpoint" address="http://localhost/mydata/DataService.svc"
                binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="MyData.Contracts.IDataService"/>
    </client>
...

文件 ServerWeb.xml 是:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:wcf="http://www.springframework.net/wcf">

  <wcf:channelFactory id="serverWebDataService"
    channelType="VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes.Contracts.IDataService, VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes"
    endpointConfigurationName="serverWebDataServiceEndpoint" />

</objects>

在应用程序中,我使用下一个代码来调用服务的方法:

IApplicationContext _ctx = ContextRegistry.GetContext();
IDataService _dataService = _ctx["serverWebDataService"] as IDataService;

var rule = _dataService.GetRuleById(ruleId);

如何从命令行使用 WCF 服务的另一个地址?

My WCF service can work on any servers. My client - is console application. In command line parameters I want set address of my WCF service.
Current in config client I have:

...
<spring>
    <context>
      <resource uri="assembly://MyAssembly.Console/MyAssembly.Console/ServerWeb.xml"/>
    </context>
  </spring>
...
<system.serviceModel>
 <client>
      <endpoint behaviorConfiguration="Default" name="serverWebDataServiceEndpoint" address="http://localhost/mydata/DataService.svc"
                binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="MyData.Contracts.IDataService"/>
    </client>
...

File ServerWeb.xml is:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:wcf="http://www.springframework.net/wcf">

  <wcf:channelFactory id="serverWebDataService"
    channelType="VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes.Contracts.IDataService, VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes"
    endpointConfigurationName="serverWebDataServiceEndpoint" />

</objects>

In application, I use next code, for call service's methods:

IApplicationContext _ctx = ContextRegistry.GetContext();
IDataService _dataService = _ctx["serverWebDataService"] as IDataService;

var rule = _dataService.GetRuleById(ruleId);

How I can use another address of WCF service from command line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

关于从前 2024-11-26 01:42:55

尝试类似的操作:

<wcf:channelFactory id="serverWebDataService"
    channelType="VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes.Contracts.IDataService, VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes"
    endpointConfigurationName="serverWebDataServiceEndpoint">
  <!-- You can use classic DI to configure the ChannelFactory<T> instance -->
  <wcf:property name="Endpoint.Address">
    <object type="System.ServiceModel.EndpointAddress, System.ServiceModel">
      <constructor-arg name="uri" value"${serviceUrl}"/>
    </object>
  </wcf:property>
</wcf:channelFactory>

您可以使用 IVariableSource 抽象从命令行获取属性值。看 :
http://www.springframework.net/doc-latest /reference/html/objects.html#objects-variablesource

<object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
   <property name="VariableSources">
      <list>
         <object type="Spring.Objects.Factory.Config.CommandLineArgsVariableSource, Spring.Core">
           <property name="ArgumentPrefix" value="--" />
           <property name="ValueSeparator" value="="/>
         </object>
      </list>
   </property>
</object>

在命令行中设置变量,如下所示:
程序.exe --serviceUrl=http://localhost/Service.svc

Try something like that :

<wcf:channelFactory id="serverWebDataService"
    channelType="VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes.Contracts.IDataService, VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes"
    endpointConfigurationName="serverWebDataServiceEndpoint">
  <!-- You can use classic DI to configure the ChannelFactory<T> instance -->
  <wcf:property name="Endpoint.Address">
    <object type="System.ServiceModel.EndpointAddress, System.ServiceModel">
      <constructor-arg name="uri" value"${serviceUrl}"/>
    </object>
  </wcf:property>
</wcf:channelFactory>

You can use IVariableSource abstraction to get a property value from commandline. See :
http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource

<object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
   <property name="VariableSources">
      <list>
         <object type="Spring.Objects.Factory.Config.CommandLineArgsVariableSource, Spring.Core">
           <property name="ArgumentPrefix" value="--" />
           <property name="ValueSeparator" value="="/>
         </object>
      </list>
   </property>
</object>

Set the variable in command line like this :
program.exe --serviceUrl=http://localhost/Service.svc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文