gpt4 book ai didi

c# - 如何摆脱 app.config 并将其全部移动到代码中?

转载 作者:行者123 更新时间:2023-11-30 20:54:37 26 4
gpt4 key购买 nike

我在这篇文章中以通用方式尝试了这个问题:https://stackoverflow.com/q/18968846/147637

但这并没有让我们得到结果。

Soooo,这里是具体的!

我有下面的代码。有用。在 VS 中,您添加一个 Web 引用,在下面编写代码,然后......开始摆弄 app.config。

而且有效。

但我需要删除应用配置。问题是代码的关键部分不在....代码中。很难记录,而且查看此示例的人很容易忘记查看应用程序配置(这是其他开发人员的示例)。

所以问题是:如何将 app.config 的内容移动到代码中?

(我是一名兼职编码员。向我指出通用文档不会让我到达那里,抱歉!)

**// .cs file:**

using myNameSpace.joesWebService.WebAPI.SOAP;

namespace myNameSpace
{
class Program
{
static void Main(string[] args)
{
// create the SOAP client
joesWebServerClient server = new joesWebServerClient();

string payloadXML = Loadpayload(filename);

// Run the SOAP transaction
string response = server.WebProcessShipment(string.Format("{0}@{1}", Username, Password), payloadXML);

=================================================
**app.config**

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<!-- Some non default stuff has been added by hand here -->
<binding name="IjoesWebServerbinding" maxBufferSize="256000000" maxReceivedMessageSize="256000000" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://joesWebServer/soap/IEntryPoint"
binding="basicHttpBinding" bindingConfiguration="IjoesWebServerbinding"
contract="myNameSpace.joesWebService.WebAPI.SOAP.IjoesWebServer"
name="IjoesWebServerSOAP" />
</client>
</system.serviceModel>
</configuration>

最佳答案

一般来说,配置文件比硬编码设置更受欢迎,因为您需要对配置文件做的就是更改您想要更改的值,然后重新启动应用程序。如果它们是硬编码的,您必须修改源代码、重新编译和重新部署。

话虽如此,您几乎可以在代码中执行在 WCF 配置文件中执行的所有操作(我似乎记得一些异常(exception)情况,但不要立即记住它们)。

实现您正在寻找的一种方法是在您的代码中定义绑定(bind)并通过 ChannelFactory<T> 创建客户端, 其中T是你的服务的接口(interface)(更准确的说是服务契约,通常在一个接口(interface)中,然后由一个类实现)。

例如:

using System.ServiceModel;
using myNameSpace.joesWebService.WebAPI.SOAP;

namespace myNameSpace
{
class Program
{
static void Main(string[] args)
{

// Create the binding
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.MaxBufferSize = 256000000;
myBinding.MaxReceivedMessageSize = 256000000;

// Create the Channel Factory
ChannelFactory<IjoesWebServer> factory =
new ChannelFactory<IjoesWebServer>(myBinding, "http://joesWebServer/soap/IEntryPoint");

// Create, use and close the client
IjoesWebService client = null;
string payloadXML = Loadpayload(filename);
string response;

try
{
client = factory.CreateChannel();
((IClientChannel)client).Open();

response = client.WebProcessShipment(string.Format("{0}@{1}", Username, Password), payloadXML);

((IClientChannel)client).Close();
}
catch (Exception ex)
{
((ICientChannel)client).Abort();

// Do something with the error (ex.Message) here
}
}
}

现在你不需要配置文件了。您在示例中的其他设置现在在代码中。

ChannelFactory<T>的优势是一旦创建了工厂实例,就可以通过调用 CreateChannel() 随意生成新 channel (将它们视为客户端) .这将加快速度,因为您的大部分开销将用于创建工厂。

附加说明 - 您正在使用 I<name>在你的配置文件的很多地方。 I通常表示一个接口(interface),如果全职开发人员要查看您的项目,乍一看可能会让他们有些困惑。

关于c# - 如何摆脱 app.config 并将其全部移动到代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18995754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com