2014-10-07 76 views
1

当我跑我的WPF使用我的WCF服务库通过Visual Studio我在同样的时间与我的服务WCF服务主机的启动开始我的WCF服务库,但是当我点击exe文件我在不启动调试文件夹WPF反正是有使之在代码开始如下面的代码我相信会的工作不。运行WCF服务库.exe文件时在调试中遇到

try 
{ 
    host = new ServiceHost(typeof(Service1), new Uri("http://" + System.Net.Dns.GetHostName() + ":8733/DatabaseTransferWcfServiceLibaryMethod/Service1/")); 
    host.Open(); 
}catch(AddressAlreadyInUseException) 
{ 
} 

我试图不使用服务引用。

回答

0

好吧,错误是从我认为是它的错误是它在我的用户界面线程上运行,所以需要添加。

[ServiceBehavior(UseSynchronizationContext = false)] 

如果有其他人有这个问题,我希望这会有所帮助。

1

我在这方面的专家,但也许你错过了绑定。这里是我可以创建和托管费在代码WCF服务的最简单的例子(您需要将引用添加到系统,System.Runtime.Serializaton和System.ServiceModel,但除此之外,这个代码完成)。

using System; 
using System.Runtime.Serialization; 
using System.ServiceModel; 

namespace ConsoleApplication1 
{ 

    class Program 
    { 

     static void Main(string[] args) 
     { 

     // Create the host on a single class 
     using 
     ( ServiceHost host 
      = new ServiceHost 
      ( typeof(MyService) 
      , new Uri("http://localhost:1234/MyService/MyService") 
      ) 
     ){ 

      // That single class could include multiple interfaces to 
      // different services, each must be added here 
      host.AddServiceEndpoint 
      ( typeof(IMyService) 
      , new WSHttpBinding(SecurityMode.None) 
       // Each service can have it's own URL, but if blank use the 
       // default above 
      , "" 
      ); 
      // Open the host so it can be consumed 
      host.Open(); 

      // Consume the service (this cuold be in another executable) 
      using 
      ( ChannelFactory<IMyService> channel 
       = new ChannelFactory<IMyService> 
       ( new WSHttpBinding(SecurityMode.None) 
       , "http://localhost:1234/MyService/MyService" 
       ) 
      ){ IMyService myService = channel.CreateChannel(); 
       Console.WriteLine(myService.GetValue()); 
      } 

      // Clean up 
      host.Close(); 

     } 


     } 

    } 

    [ServiceContract] 
    public interface IMyService 
    { [OperationContract] int GetValue(); 
    } 

    public class MyService : IMyService 
    { public int GetValue() 
     { return 5; 
     } 
    } 

} 
+0

我在结合加入,但它冻结起来,变为不响应在等效“Console.WriteLine(myService.GetValue());” – ZoomVirus 2014-10-08 07:58:19

+0

@ZoomVirus它会冻结我发送的示例代码吗?当它冻结在您的等效代码中时,处于无限冻结状态,或超时。你有什么例外吗?出 – Eliott 2014-10-08 08:41:24

+0

这时候我跑在Visual Studio调试,但如果我点击等待它出从未回应它倍。你放的示例代码也是一样的。 – ZoomVirus 2014-10-08 08:46:15