WCF 服务 - 运行时在接口上看不到 ServiceContract

发布于 2024-07-14 04:52:01 字数 1085 浏览 5 评论 0原文

我是 WCF 新手,正在尝试运行我的第一个服务。 我很接近,但仍停留在这个问题上。

在我的接口定义文件中,我有这个:

[ServiceContract(Namespace="http://mysite.com/wcfservices/2009/02")]       
    public interface IInventoryService
    {
        [OperationContract]
        string GetInventoryName(int InventoryID);
    }

然后我有继承它的类文件(用于服务):

   public class InventoryService : IInventoryService
    {
        // This method is exposed to the wcf service
        public string GetInventoryName(int InventoryID)
        {
            return "White Paper";
        }

最后,在我的主机项目中,我有这个:

    ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
    host.AddServiceEndpoint(typeof(Inventory.InventoryService), new NetTcpBinding(),
        "net.tcp://localhost:9000/GetInventory");
    host.Open();

一切都编译良好,当主机添加服务端点时,它轰炸了这样的内容:“合同类型 Inventory.InventoryService 不具有 ServiceContractAttribute 属性。为了定义有效的合同,指定的类型(合同接口或服务类)必须具有 ServiceContractAttribute 属性。”

我知道我在这里缺少一些简单的东西。 我将接口明确标记为服务合同,并且在主机项目中引用了该项目。

I'm new to WCF and trying to get my first service running. I'm close but stuck on this problem.

In my interface definition file, I have this:

[ServiceContract(Namespace="http://mysite.com/wcfservices/2009/02")]       
    public interface IInventoryService
    {
        [OperationContract]
        string GetInventoryName(int InventoryID);
    }

Then I have my class file (for the service) that inherits it:

   public class InventoryService : IInventoryService
    {
        // This method is exposed to the wcf service
        public string GetInventoryName(int InventoryID)
        {
            return "White Paper";
        }

Finally, in my Host project I have this:

    ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
    host.AddServiceEndpoint(typeof(Inventory.InventoryService), new NetTcpBinding(),
        "net.tcp://localhost:9000/GetInventory");
    host.Open();

Everything compiles fine, and when the host goes to add the service endpoint, it bombs with this: "The contract type Inventory.InventoryService is not attributed with ServiceContractAttribute. In order to define a valid contract, the specified type (either contract interface or service class) must be attributed with ServiceContractAttribute."

I know I'm missing something simple here. I have the interface clearly marked as a service contract and there's a reference to that project in the Host project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2024-07-21 04:52:01
ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.InventoryService), new NetTcpBinding(),
    "net.tcp://localhost:9000/GetInventory");
host.Open();

如果您的 ServiceContract 属性位于接口而不是具体类上,请尝试以下操作:

ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.IInventoryService), new NetTcpBinding(),
    "net.tcp://localhost:9000/GetInventory");
host.Open();
ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.InventoryService), new NetTcpBinding(),
    "net.tcp://localhost:9000/GetInventory");
host.Open();

If your ServiceContract attribute is on the Interface not the concrete class, try the following:

ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.IInventoryService), new NetTcpBinding(),
    "net.tcp://localhost:9000/GetInventory");
host.Open();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文