【问题标题】:How to disable auto-generated WCF configuration如何禁用自动生成的 WCF 配置
【发布时间】:2011-02-25 18:30:07
【问题描述】:

每次我的程序运行时,vs 都会将默认配置添加到我的 app.config 文件中。在那次运行时它运行良好,但在下一次运行时它实际上会尝试读取配置。

问题是默认配置有错误,它添加了属性“地址”,但属性不允许有大写,所以会抛出异常。

这意味着我每次运行都必须删除坏部分!

我已尝试配置 .config,但出现错误。

这是我用来托管服务器的代码:

private static System.Threading.AutoResetEvent stopFlag = new System.Threading.AutoResetEvent(false);
ServiceHost host = new ServiceHost(typeof(Service), new Uri("http://localhost:8000"));
host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "ChessServer");
host.Open();
stopFlag.WaitOne();
host.Close();

这是调用服务器的客户端代码:

ChannelFactory<IChessServer> scf;
scf = new ChannelFactory<IService>
              (new BasicHttpBinding(), "http://localhost:8000");
IService service = scf.CreateChannel();

感谢您的帮助。

编辑:对不起,我花了这么长时间,我一直在尝试使用 DualWSHttpBinding 代替(因为我实际上需要服务器调用客户端方法)但仍然生成配置文件。这是整个自动生成的配置文件:

<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="Chess.ChessService">
                <endpoint Address="" binding="wsHttpBinding" contract="Chess.IChessServer">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint Address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <Add baseAddress="http://localhost:8732/Design_Time_Addresses/Chess/ChessService/" />
                    </baseAddresses>
                </host>
            </service>
            <service name="Chess.ChessClient">
                <endpoint Address="" binding="wsHttpBinding" contract="Chess.IChessClient">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint Address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <Add baseAddress="http://localhost:8732/Design_Time_Addresses/Chess/ChessClient/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

【问题讨论】:

  • 啊,是的,谢谢,在这一点上有点迷失:P

标签: c# wcf


【解决方案1】:

你大错特错了。属性和元素名称可以是大写或小写。

是什么让你认为属性的大小写是问题所在?是什么让您认为 app.config 在每次运行时都会更改?

【讨论】:

  • 因为这个错误:“无法识别的属性'地址'。请注意,属性名称区分大小写。” (C:\school\programming\C#\Server\bin\Debug\Server.vshost.exe.Config 第 14 行)而且因为每次运行后我都会收到一条消息,说明配置文件已更改,如果我想重新加载它。它可能会更改配置,因为我将配置留空。但我不想使用任何配置。我想根据用户输入而不是静态配置来托管服务器。
  • 如果属性无法识别,那么它是由于某种原因而不是大小写。为什么不发布 WCF 生成的 XML?
【解决方案2】:

Visual Studio 不会在每次运行时重新创建 WCF 配置。每次您在项目中的服务引用上执行Update Service Reference 时,它都会重新创建 WCF 配置 - 但它绝对不会在每次运行之前自动执行此操作 - 这里一定有其他原因让您感到悲伤。

此外,您没有连接到正确的地址 - 您的服务器在这里定义了它:

ServiceHost host = new ServiceHost(..., new Uri("http://localhost:8000"));
host.AddServiceEndpoint(..., .., "ChessServer");

这会导致您在服务器上的端点地址是

http://localhost:8000/ChessServer

但是,您的客户端似乎尝试连接到

http://localhost:8000/

那里没有服务。

最后一点:如果您在代码中设置了端点、绑定等所有内容,那么对配置的任何更改都不会打扰您 - 一定是其他原因导致您的问题。

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 2017-05-12
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多