- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这篇文章中以通用方式尝试了这个问题: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/
我想在不使用函数 m 中的循环的情况下加快计算并获得结果.可重现的例子: N require(rbenchmark) > benchmark(m(r, N), + m1(r, N
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我正在尝试使用 Cython。我使用 setup.py 并构建,而不是让 pyximport 执行此操作。但是,每次我导入模块时,似乎都会调用 pyximport 。 Pyximport 构建失败,一
考虑两个案例 case1 和 case2 以及两个方法 method1 和 method2。假设方法 1 解决案例 1,方法 2 解决案例 2。现在,我有一个程序可能以 case1 或 case2 结
我怎样才能摆脱 CA2202 警告(CA2202:Microsoft.Usage:对象“compressedStream”可以在方法“Compression.InternalDecompress(by
我在这段代码中得到 NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE final Integer id = Ints.tryParse(idString); FailRea
我的 eclipse 中有 Java WebService 代码。我用过@WebService @Webmethod、@XmlElements、@XmlType、@XmlAccessorType 现在
在我正在编写的基于 Sprite 的游戏中,二维网格中的每个字段都包含一堆 Sprite 。大多数情况下,最重要的是最重要的。 在游戏的规则模块中,我有很多这样的代码: public boolean
在 Java 中,我必须设置一个带有值的 POJO 类。然而,要决定使用哪个 setter 函数,我必须取决于 if 条件。我当前的代码如下所示: // Code written in a funct
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭去年。 Improve this
所以我最近接触到了C++中所谓“令人厌恶的函数类型”的怪诞之处(据我所知源自这篇论文: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015
我正在研究配置 QDialog。它有几个类别(General、Appearance 等),当用户点击它们时会加载它们。每个类别都有自己的页面。这些页面本身就是独立的类(每个都有自己的 ui、cpp 和
我一直在开发 Vb.Net应用程序最近,我试图使它尽可能轻量级(即,使二进制文件尽可能小)。 我已经完成了所有琐碎的工作,但是在使用 ILDasm 浏览二进制文件时,我注意到它有一个 My names
An easy way to get rid of *everything* generated by SBT?要求一个简单的方法来清理从 sbt 生成的所有文件,但没有找到。我会要求一个艰难的。我如
如何摆脱默认的 ANTLR 识别错误? 我想用我自己的错误类而不是 ANTLR 的错误来写另一条消息。 我的意思是是否有可能扩展某些 ANTLR 错误类以显示我自己的消息? 更清楚地说,我不想在控制台
使用 woocommerce 的订单面板时,我注意到使用搜索执行了不必要的查询。这是对 Woocommerce 文件(/includes/data-stores/class-wc-order-data
我有一个包含列的大数据框,例如: ID, time, OS, IP 该数据帧的每一行对应一个条目。在某些 ID 的数据框中,存在多个条目(行)。我想摆脱那些多行(显然,对于相同的 ID,其他属性会有所
当我运行测试时,我得到如下代码: objc[8845]: GC: forcing GC OFF because OBJC_DISABLE_GC is set 它还会污染我运行的子流程的输出。我如何摆脱
在 ie8 上,状态栏下方有一个绿色进度指示器,可能表示基于某处某个静态长度值的下载进度。不幸的是,由于“现代”动态 JavaScript、ajax 调用等的性质,该指示器经常会变得困惑,并且栏保持在
我现在正在学习 MVVM,但我了解的东西很少(这里有很多但很少..): 是否每个模型都可能(通过 VM)暴露给 View 有一个 VM? 例如,如果我有一个 Contact 和 Address 实体并
我是一名优秀的程序员,十分优秀!