【问题标题】:Run WCF Service Library when .exe in debug is ran运行调试中的 .exe 时运行 WCF 服务库
【发布时间】:2014-10-07 09:04:28
【问题描述】:

当我通过 Visual Studio 运行使用我的 WCF 服务库的 WPF 时,我在启动 WCF 服务库的同时启动了 WCF 服务主机,但是当我在调试中单击我的 WPF 的 exe 时文件夹它不启动无论如何让它在代码中启动,因为我认为以下代码不起作用。

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

我正在尝试不使用服务引用。

【问题讨论】:

    标签: c# .net wpf wcf


    【解决方案1】:

    我不是这方面的专家,但也许你错过了绑定。这是我可以创建的在代码中托管和使用 WCF 服务的最简单示例(您需要添加对 System、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;
          }
       }
    
    }
    

    【讨论】:

    • 我已在绑定中添加,但它冻结并在等效的 'Console.WriteLine(myService.GetValue());' 处变得不响应
    • @ZoomVirus 在我发送的示例代码中是否冻结?当它冻结在您的等效代码时,处于无限冻结状态或超时。你有什么例外吗?
    • 当我在 Visual Studio 中运行调试时它会超时,但如果我点击等待它响应它永远不会超时。你放的示例代码也是如此。
    • @ZoomVirus 抱歉,我对此知之甚少,无法回复。我只知道我发送的代码对我有用。如果你不能创建自己的主机,你有 IIS 吗?你可以在那里托管你的 WCF 库。
    • 很遗憾,使用 IIS 不符合我的要求,但感谢您的帮助。
    【解决方案2】:

    好的,错误与我认为的错误相去甚远,因为它正在我的用户界面线程上运行,因此需要添加。

    [ServiceBehavior( UseSynchronizationContext = false)]
    

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

    【讨论】:

      猜你喜欢
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多