没有DataService的WCF oData

时间:2015-01-27 22:07:15

标签: c# .net wcf odata wcf-data-services

我必须提供一个RESTful oData服务: - 基于WCF(而不是ASP.NET Web API)
- 在自托管应用程序中运行
- 拥有自定义数据源
- 可以返回任何在设计时未知的类型

目前我能够提供RESTful服务(无法拥有自定义数据源):

internal class Program
{
    private static readonly Uri BaseAddress = new Uri("http://localhost:8000");

    private static void Main()
    {
        LaunchWebServiceHost();
    }

    private static void LaunchWebServiceHost()
    {
        var webServiceHost = new WebServiceHost(typeof (Service), BaseAddress);

        var binding = new WebHttpBinding();
        var endpoint = webServiceHost.AddServiceEndpoint(typeof (IServiceContract), binding, "api");
        endpoint.Behaviors.Add(new WebHttpBehavior());

        webServiceHost.Description.Behaviors.Add(new ServiceMetadataBehavior {HttpGetUrl = BaseAddress, HttpGetEnabled = true});

        webServiceHost.Open();

        Console.WriteLine("Host is ready...");
        Console.ReadLine();

        webServiceHost.Close();
    }
}

[ServiceContract]
public interface IServiceContract
{
    [WebGet(UriTemplate = "{*type}/")]
    [OperationContract]
    IEnumerable<Car> GetData(string type);
}

public class Service : IServiceContract
{
    public IEnumerable<Car> GetData(string type)
    {
        // Get resources of requested "type"
        // For development just return cars...

        return new[]
        {
            new Car
            {
                Id = "12345",
                Name = "VW Polo",
                Color = "Green"
            },
            new Car
            {
                Id = "00001",
                Name = "Audi A3",
                Color = "Blue"
            },
            new Car
            {
                Id = "99999",
                Name = "BMW 130i",
                Color = "Red"
            }
        };
    }
}

public class Car : BusinessObjectBase
{
    public string Name { get; set; }
    public string Color { get; set; }
}

public class BusinessObjectBase
{
    public string Id { get; set; }
}

我找不到符合我要求的任何东西。有人给我一个提示吗?

谢谢和最好的问候 拉曼

0 个答案:

没有答案