如何禁用自动生成的WCF配置

时间:2010-05-30 19:50:35

标签: c# wcf

每次我的程序运行时,都会将默认配置添加到我的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>

2 个答案:

答案 0 :(得分:1)

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

是什么让你认为属性的情况是问题?是什么让你认为每次运行都会改变app.config?

答案 1 :(得分:1)

Visual Studio在每次运行时都会重新创建WCF配置。每次在项目中对服务引用执行Update Service Reference时,它都会重新创建WCF配置 - 但它确实不会在每次运行之前自动执行此操作 - 必须有其他东西让您感到悲伤。< / p>

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

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

这会导致服务器上的端点地址为

http://localhost:8000/ChessServer

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

http://localhost:8000/

那里没有服务。

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

相关问题