简体   繁体   English

具有相同类的自定义ServiceContract的WCF数据服务

[英]WCF Data Service with a custom ServiceContract in the same class

I have a problem very similar to this: Combine ADO.NET Data Service and custom ServiceContract in the same class? 我有一个非常类似于此的问题:将ADO.NET数据服务和自定义ServiceContract合并在同一类中吗?

(However, I can't comment on that, since i don't have 50 reputation... therefore I have to open a new question) (但是,我无法对此发表评论,因为我没有50个声誉,因此我不得不提出一个新问题)

So, I have a WCF Data Service: 因此,我有一个WCF数据服务:

public class LearnDataService : DataService<LearnEntities>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Customers", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
}

I'd like to extend it with a few custom WCF Service operations (they will be doing complex DB queries in real life, i'd be using simple calls instead just to keep the example code short). 我想通过一些自定义的WCF服务操作来扩展它(它们将在现实生活中进行复杂的数据库查询,而我将使用简单的调用来代替,以使示例代码简短)。 So, I created the interface: 因此,我创建了接口:

[ServiceContract]
public interface ICustomerService
{
    [OperationContract]
    List<Customer> GetCustomers();

    [OperationContract]
    Customer GetCustomerByCustomerId(string customerID);
}

Extended the service class like this: 扩展服务类如下:

public class LearnDataService : DataService<LearnEntities>, ICustomerService
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Customers", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;


        config.SetServiceOperationAccessRule("GetCustomers", ServiceOperationRights.AllRead);
        config.SetServiceOperationAccessRule("GetCustomerByCustomerId", ServiceOperationRights.AllRead);
    }

    [WebGet]
    public List<Customer> GetCustomers()
    {
        var customers = (from c in this.CurrentDataSource.Customers
                       select c).ToList();

        return customers;
    }

    [WebGet]
    public Customer GetCustomerByCustomerId(string customerID)
    {
        var product = (from c in this.CurrentDataSource.Customers
                       where c.CustomerID == customerID
                       select c).FirstOrDefault(); ;

        return product;
    }
}

Now, when trying to use the service, I get: 现在,当尝试使用该服务时,我得到:

LearnDataService implements multiple servicecontract types, and no endpoints are defined in the configuration file. LearnDataService实现多种服务合同类型,并且在配置文件中未定义任何终结点。

Ok, as said in the answer for the question I linked above, I set the config file as follows: 好的,正如我在上面链接的问题的答案中所述,我将配置文件设置如下:

<services>
  <service name="LearnService.LearnDataService">
    <endpoint address="data" binding="webHttpBinding" contract="System.Data.Services.IRequestHandler" />
    <endpoint address="cust" binding="webHttpBinding" contract="LearnService.ICustomerService" />
  </service>
</services>

Now, I'm trying to add a service reference for this service in a client project. 现在,我正在尝试在客户端项目中为此服务添加服务引用。 Using "Add Service Reference..." and pressing Discover button, (of course with the Service running without debugging), it discovers the ht_tp://localhost:7947/LearnDataService.svc url, and I get a 404 (no wonder, there's nothing listening there...) 使用“添加服务引用...”并按“发现”按钮(当然,该服务无需调试即可运行),它会发现ht_tp:// localhost:7947 / LearnDataService.svc网址,我得到了404(难怪,那里什么也没听...)

Since I don' have 10 reputation, I can't post more than 2 links. 由于我没有10个声誉,因此我发布的链接不能超过2个。 This includes localhost links, so I have to mangle them this way :( sorry 这包括本地主机链接,所以我必须这样处理它们:(对不起

Now, I modify the Address to ht_tp://localhost:7947/LearnDataService.svc/data, then it correctly discovers the LearnEntities service, and when opening it, I can see the Customers entity. 现在,我将地址修改为ht_tp:// localhost:7947 / LearnDataService.svc / data,然后它会正确发现LearnEntities服务,并且在打开该服务时,我可以看到“客户”实体。 So far so good, so I check the other service, ht_tp://localhost:7947/LearnDataService.svc/cust. 到目前为止一切顺利,因此我检查了另一个服务ht_tp:// localhost:7947 / LearnDataService.svc / cust。 When discovering that, I get an error: 发现该错误时,出现错误:

There was an error downloading 'ht_tp://localhost:7947/LearnDataService.svc/cust/_vti_bin/ListData.svc/$metadata'. 下载“ ht_tp:// localhost:7947 / LearnDataService.svc / cust / _vti_bin / ListData.svc / $ metadata”时出错。 The request failed with HTTP status 404: Not Found. 请求失败,HTTP状态为404:找不到。 Metadata contains a reference that cannot be resolved: 'ht_tp://localhost:7947/LearnDataService.svc/cust'. 元数据包含无法解析的引用:“ ht_tp:// localhost:7947 / LearnDataService.svc / cust”。 There was no endpoint listening at http://localhost:7947/LearnDataService.svc/cust that could accept the message. http:// localhost:7947 / LearnDataService.svc / cust上没有侦听终结点的端点可以接受该消息。 This is often caused by an incorrect address or SOAP action. 这通常是由不正确的地址或SOAP操作引起的。 See InnerException, if present, for more details. 有关更多详细信息,请参见InnerException(如果存在)。 The remote server returned an error: (404) Not Found. 远程服务器返回错误:(404)找不到。 If the service is defined in the current solution, try building the solution and adding the service reference again. 如果服务是在当前解决方案中定义的,请尝试构建解决方案并再次添加服务引用。

When trying these URLs using a browser, the ht_tp://localhost:7947/LearnDataService.svc/data works, and shows the data in XML format, but the URL ht_tp://localhost:7947/LearnDataService.svc/cust gives a 404: (Endpoint not found.) 使用浏览器尝试这些URL时,ht_tp:// localhost:7947 / LearnDataService.svc / data可以工作,并以XML格式显示数据,但是URL ht_tp:// localhost:7947 / LearnDataService.svc / cust提供了404 :(找不到端点。)

Do you have any idea, what is the problem with the configuration? 您有什么想法,配置有什么问题? Am I understanding the answer of the linked SO question correctly? 我是否正确理解链接的SO问题的答案?

Finally, I found the solution. 最后,我找到了解决方案。 Apparently, there is no way to get this to work by adding standard WCF service queries to a Data Service like I tried above, but it turns out that WCF Data Services has a (somewhat limited) support for writing custom queries, which is called WCF Data Service Operations : 显然,没有办法像我上面尝试的那样通过向数据服务中添加标准WCF服务查询来使其工作,但是事实证明WCF数据服务对编写自定义查询提供了(某种程度上的)支持,这称为WCF。数据服务运营

http://blogs.msdn.com/b/odatateam/archive/2010/05/26/service-operations-and-the-wcf-data-services-client.aspx https://msdn.microsoft.com/en-us/library/cc668788(v=vs.110).aspx http://blogs.msdn.com/b/odatateam/archive/2010/05/26/service-operations-and-the-wcf-data-services-client.aspx https://msdn.microsoft.com/en -us / library / cc668788(v = vs.110).aspx

This way I could accomplish what I wanted to do in the first place. 这样,我就可以完成我想做的事情。 However, this solution isn't as nice as it shoud be, since there's no service contract defined for these operations, they won't show up in WSDL, no automatic client code generation for them, etc. You have to know the names and parameters of these operations when writing the client code. 但是,此解决方案并不理想,因为没有为这些操作定义服务合同,因此它们不会显示在WSDL中,也不会为它们自动生成客户端代码,等等。您必须知道名称和编写客户端代码时这些操作的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一个类中结合ADO.NET数据服务和自定义ServiceContract? - Combine ADO.NET Data Service and custom ServiceContract in the same class? 我可以在WCF ServiceContract中公开数据成员吗? - Can I expose a data member in a WCF ServiceContract? ASP.NET Web服务的ServiceContract类 - ServiceContract class for an ASP.NET web service 配置WCF,其中ServiceContract和服务实现位于单独的程序集中 - Configure WCF where ServiceContract and service implementation are in separate assemblies WCF ServiceContract上的“名称”属性可以用于更改服务的URI吗? - Can the “Name” attribute on a WCF ServiceContract be used to change the service's URI? 共享WCF服务和客户端之间的接口(标记为W / ServiceContract) - Sharing Interfaces Between a WCF Service and Client (marked w/ ServiceContract) 从单个服务托管多个WCF ServiceContract实现 - Hosting multiple WCF ServiceContract implementations from a single service WCF,将数据传递给服务类的自定义授权 - WCF, custom authorization with data being passed to service class 无法从WCF数据服务返回自定义类 - Unable to return custom class from WCF Data Service 如何访问自定义类对象并从WCF服务更改其数据? - How to access custom class objects and change their data from a WCF service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM