我的第一个WCF服务器 - 收到InvalidOperatonException..NET声称缺少ServiceContract属性

jav*_*red 0 c# wcf

public interface IConsoleData
{
    double GetCurrentIndicator();
}

public class IConsoleDataImpl : IConsoleData
{
    public double GetCurrentIndicator()
    {
        return 22;
    }
}

[ServiceContract]
public interface IMBClientConsole
{
    [OperationContract]
    IConsoleData GetData();
}

public class MBClientConsole : IMBClientConsole
{
    public IConsoleData GetData()
    {
        return new IConsoleDataImpl();
    }
}

class Log
{

    public static void initialize()
    {
        using (ServiceHost host = new ServiceHost(typeof(MBClientConsole),
          new Uri[]{
      new Uri("net.pipe://localhost")
    }))
        {
            host.AddServiceEndpoint(typeof(MBClientConsole),
              new NetNamedPipeBinding(),
              "PipeReverse");

            host.Open();
            // TODO: host.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

AddServiceEndpoint我接到电话时

合同类型Commons.MBClientConsole不归属于ServiceContractAttribute.为了定义有效的合同,必须使用ServiceContractAttribute来声明指定的类型(合同接口或服务类).

但正如您从我的代码界面中看到的那样,归因于ServiceContract属性.为什么VS声称它没有以及如何解决问题?

下面的更新列表:

public interface IConsoleData
{
    double GetCurrentIndicator();
}

public class IConsoleDataImpl : IConsoleData
{
    public double GetCurrentIndicator()
    {
        return 22;
    }
}

[ServiceContract]
public interface IMBClientConsole
{
    [OperationContract]
    IConsoleData GetData();
}

public class MBClientConsole : IMBClientConsole
{
    public IConsoleData GetData()
    {
        return new IConsoleDataImpl();
    }
}

class Log
{

    private ServiceHost _host;

    public void initialize()
    {
        _host = new ServiceHost(typeof (MBClientConsole),
                                new Uri[]
                                    {
                                        new Uri("net.pipe://localhost")
                                    });
            _host.AddServiceEndpoint(typeof(IMBClientConsole),
              new NetNamedPipeBinding(),
              "PipeReverse");

            _host.Open();
            System.Threading.Thread.Sleep(1000000);
            // TODO: host.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

客户:

public interface IConsoleData
{
    double GetCurrentIndicator();
}

[ServiceContract]
public interface IMBClientConsole
{
    [OperationContract]
    IConsoleData GetData();
}

class Program
{
    static void Main(string[] args)
    {
        ChannelFactory<IMBClientConsole> pipeFactory =
            new ChannelFactory<IMBClientConsole>(
                new NetNamedPipeBinding(),
                new EndpointAddress(
                    "net.pipe://localhost/PipeReverse"));

        IMBClientConsole pipeProxy =
          pipeFactory.CreateChannel();

        while (true)
        {
            string str = Console.ReadLine();
            Console.WriteLine("pipe: " +
              pipeProxy.GetData().GetCurrentIndicator());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我仍然收到CommunicationException"从管道读取错误:通道已关闭.(109,0x6d)."

Car*_*ten 5

更改

host.AddServiceEndpoint(typeof(MBClientConsole),
              new NetNamedPipeBinding(),
              "PipeReverse");
Run Code Online (Sandbox Code Playgroud)

host.AddServiceEndpoint(typeof(IMBClientConsole),
              new NetNamedPipeBinding(),
              "PipeReverse");
Run Code Online (Sandbox Code Playgroud)

第二个需要服务合同(=接口)而不是实现.有时它们是相同的但不在这里.

using (ServiceHost host = new ServiceHost(typeof(MBClientConsole),
  new Uri[]{new Uri("net.pipe://localhost")}))
Run Code Online (Sandbox Code Playgroud)

你说WCF MBClientConsole在调用服务时(和服务的基地址)创建()对象是AddServiceEndpoint什么,但是你告诉WCF要公开什么样的服务合同.我希望你能从中得到任何意义;)

你的代码中有一个错误:

public static void initialize()
{
    using (ServiceHost host = new ServiceHost(typeof(MBClientConsole),
      new Uri[]{new Uri("net.pipe://localhost")}))
    {
        host.AddServiceEndpoint(typeof(IMBClientConsole),
          new NetNamedPipeBinding(),
          "PipeReverse");

        host.Open();
        // TODO: host.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

因为你正在使用using(Service host ...主机对象,所以如果你使用Initialize-Method,它将被处理掉.我想你是从ConsoleHost-Example中复制过来的.将主机移动到一个字段并使用以下内容:

class Log
{

    ServiceHost _host;
    public void Initialize()
    {
      _host = new ServiceHost(typeof(MBClientConsole),
          new Uri[]{new Uri("net.pipe://localhost")});

      _host.AddServiceEndpoint(typeof(IMBClientConsole),
              new NetNamedPipeBinding(),
              "PipeReverse");

      _host.Open();
    }

    public void Close()
    {
       _host.Close();
       _host.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

备注:你让一切都变得静止(按照我的说法复制) - 我删除了它.您甚至可以考虑实施IDisposable(推荐),但我会将此留给您.你必须使用这个略有不同的

var log = new Log();
log.Initialize();
Run Code Online (Sandbox Code Playgroud)

在退出程序之前:

log.Close();
Run Code Online (Sandbox Code Playgroud)