2010-05-30 52 views
1

每当我的程序运行vs将默认配置添加到我的app.config文件。在这种情况下,它运行良好,但在下一次运行时,它实际上会尝试读取配置。如何禁用自动生成的WCF配置

的问题是,默认的配置有错误,它增加了属性“地址”,但attritbutes不允许有首都所以它抛出一个异常。

这意味着我必须在每次运行中删除坏段!

我试过配置.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> 
+0

啊是的,谢谢,在这一点上有点失落:P – MrFox 2010-05-31 14:18:14

回答

1

的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/ 

并且没有服务那里。

最后一点:如果你设置你所有的东西一样端点,绑定等代码,到配置的任何更改甚至不应该打扰你的话 - 必须有别的东西造成你的问题。

1

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

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

+0

由于此错误: “无法识别的属性'地址'。请注意,属性名称区分大小写。 (C:\ school \ programming \ C#\ Server \ bin \ Debug \ Server.vshost.exe.Config第14行) 因为我在每次运行后都会收到一条消息,它。 它可能会改变配置,因为我离开配置为空。 但我不想使用任何配置。我想基于用户输入而不是静态配置来托管服务器。 – MrFox 2010-05-30 21:25:52

+0

如果该属性无法识别,那么除了大小写外,这是出于某种原因。为什么不发布WCF生成的XML? – 2010-06-01 06:24:56