【问题标题】:WCF REST partial class/interface - endpoint not found issueWCF REST 部分类/接口 - 找不到端点问题
【发布时间】:2013-01-22 03:48:50
【问题描述】:

以下代码导致“找不到服务端点”错误。它使用部分类和部分接口。当我不使用部分类/接口时,它可以正常工作......有任何错误吗?

[ServiceContract]
public partial interface IMySvc
{
    [WebGet(UriTemplate = "...")]
    [OperationContract]
    Stream GetProducts_1();

    [WebGet(UriTemplate = "...")]
    [OperationContract]
    Stream GetProducts_2();
}

public partial interface IMySvc
{
    [WebGet(UriTemplate = "...")]
    [OperationContract]
    Stream GetProducts_3();

    [WebGet(UriTemplate = "...")]
    [OperationContract]
    Stream GetProducts_4();
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public partial class MySvc: IMySvc
{
    //implementations of GetProducts_1, GetProducts_2
}

public partial class MySvc: IMySvc
{
    //implementations of GetProducts_3, GetProducts_4
}

在 Global.asxc 中:

RouteTable.Routes.Add(new ServiceRoute("task", new WebServiceHostFactory(), typeof(MySvc)));

【问题讨论】:

    标签: c# asp.net wcf rest wcf-rest


    【解决方案1】:

    这可能与它是部分类无关。这可能是许多不同的问题。您需要缩小问题的来源。试试:

    • 首先构建一个简单的基本 http Web 服务。
    • 当您开始工作时,将其更改为休息服务。

    【讨论】:

    • 谢谢,但是当我不使用部分类时,它可以工作。所以我知道它必须使用部分类/接口。
    【解决方案2】:

    这里是我的例子

    这将是第一组方法

    [ServiceContract]
    public partial interface IDefaultInterface
    {
        [OperationContract]
        string getData1();
    }
    
    public partial class CDefaultClass : IDefaultInterface
    {
        public getData1(){ return "data 1"; }
    }
    

    这将是您想要拆分的另一组方法

    [ServiceContract]
    public partial interface IDefaultInterface2
    {
        [OperationContract]
        string getData2();
    }
    
    public partial class CDefaultClass2 : IDefaultInterface2
    {
        public getData2(){ return "data 2"; }
    }
    

    以下是我与 cmets 的衍生逻辑

    namespace wcfMyService
    {
        // this list all derivation for the easy to follow service.
        // this service will have LOTS of function so ability
        // to split in multiple classes was necessary
        // on client side we only see 1 set of function all mixed together
        // but at least working on it it's easy to follow for us since we have a structure
    
        #region Class Derivation
    
        public class CDerivative : CDefaultClass { }
    
        public partial class CDefaultClass : CDefaultClass2 { }  
    
        // NOTE THAT ALL NEW CLASSES MUST :
        // - Be PARTIAL classes
        // - Implement their OWN interface
    
        // new class would be
        // public partial class CDefaultClass2 : CMyNewClass { }
        // and so on. previous class derive from the new class
    
        #endregion
    
        #region Interface Derivation
    
        [ServiceContract]
        public interface IDerivative : IDefaultInterface { }
    
        public partial interface IDefaultInterface : IDefaultInterface2 { }
    
        // NOTE THAT ALL NEW INTERFACE MUST :
        // - Be PARTIAL Interface
        // - Have attribute [ServiceContract]
        // - all methods need [OperationContract] as usual
    
        // new class interface would be
        // public partial interface IDefaultInterface2 : IMyNewClass { }
        // and so on. previous class interface derive from the new class interface
    
        #endregion
    }
    

    现在您的普通服务接口只需添加派生 AND 接口,以便端点查看所有派生类的所有功能。内部不需要代码,因为 Class 从它们各自的接口实现它们自己的方法。所以每个类中没有数百万个方法的集群

    [ServiceContract]
    public interface IService : IDerivative
    {
    
    }
    

    最后,您需要将 SVC/ASMX(无论如何)编码如下,以便为 WSDL 等公开所有内容。同样在这个类中也不需要代码

    public partial class Service : CDerivative, IService
    {       
    }
    

    因此,总体而言,当您希望在另一个类中使用新方法以获得更好的结构时,您只需创建部分接口和部分类,就像通常创建服务合同和操作一样。然后只需进入派生文件并将您的接口和类添加到派生中。因为它们都只实现了自己的接口,所以不应该有任何冲突,而且当您编写代码时,您将永远不会看到其他类的方法。

    我非常喜欢这个组织,并使用适当的文件夹制作漂亮的树形视图,我可以快速从那里的数千个方法中找到一个方法。

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 2011-04-11
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多