C# 具有自定义WCF 4.5 WebHttpBehavior的UriTemplates

C# 具有自定义WCF 4.5 WebHttpBehavior的UriTemplates,c#,.net,wcf,rest,uritemplate,C#,.net,Wcf,Rest,Uritemplate,我正在实现一个自定义WCF REST行为,它实现/覆盖基本WebHttpBehavior,但允许使用自定义序列化程序进行REST通信。代码基于Carlos的工作 我已经让它运行了,但问题是我们真的希望使用UriTemplate功能来允许真正的REST-ful URI。是否有人看到这一点,或者可以提供帮助以找到正确的实现 我们坚持使用WCF是为了同时提供REST和SOAP端点,所以这里不选择使用Web API。问题可能只是这个示例有点过时。此外,实现类NewtonsoftJsonBehavior在

我正在实现一个自定义WCF REST行为,它实现/覆盖基本WebHttpBehavior,但允许使用自定义序列化程序进行REST通信。代码基于Carlos的工作

我已经让它运行了,但问题是我们真的希望使用UriTemplate功能来允许真正的REST-ful URI。是否有人看到这一点,或者可以提供帮助以找到正确的实现


我们坚持使用WCF是为了同时提供REST和SOAP端点,所以这里不选择使用Web API。

问题可能只是这个示例有点过时。此外,实现类NewtonsoftJsonBehavior在
验证(ServiceEndpoint)
方法中显式重写并引发
InvalidOperationException

使用,删除验证:

public override void Validate(ServiceEndpoint endpoint)
{
    base.Validate(endpoint);

    //TODO: Stop throwing exception for default behavior.
    //BindingElementCollection elements = endpoint.Binding.CreateBindingElements();
    //WebMessageEncodingBindingElement webEncoder = elements.Find<WebMessageEncodingBindingElement>();
    //if (webEncoder == null)
    //{
    //    throw new InvalidOperationException("This behavior must be used in an endpoint with the WebHttpBinding (or a custom binding with the WebMessageEncodingBindingElement).");
    //}

    //foreach (OperationDescription operation in endpoint.Contract.Operations)
    //{
    //    this.ValidateOperation(operation);
    //}
}
服务
类中,添加一个简单的实现来验证参数是否被解析:

public Person GetPerson(string lastName)
{
    return new Person
    {
        FirstName = "First",
        LastName = lastName, // Return the argument.
        BirthDate = new DateTime(1993, 4, 17, 2, 51, 37, 47, DateTimeKind.Local),
        Id = 0,
        Pets = new List<Pet>
        {
            new Pet { Name= "Generic Pet 1", Color = "Beige", Id = 0, Markings = "Some markings" },
            new Pet { Name= "Generic Pet 2", Color = "Gold", Id = 0, Markings = "Other markings" },
        },
    };
}

我开始实现我自己的
UriTemplate
解析/匹配逻辑,但后来我偶然发现了这个答案(),并发现它可以实现更多功能


为了使用它,您仍然必须取消注释与验证未使用
UriTemplate
相关的代码。最后,为了我的目的,我重新格式化了代码(拿出逻辑来检查是否有多个参数,因为在我的用例中,主体始终只有一个参数)。

您是如何托管WCF服务的?您使用的是什么绑定?我们通过IIS托管,通过代码激活服务。我们有一个自定义的REST和SOAP服务主机工厂设置,允许我们更改行为、绑定等,然后使用标准的WCF路由服务激活来使用REST和SOAP工厂加载每个服务。那么您的问题到底是什么?嘿@Paul,问题是我不知道如何重新实现UriTemplate功能。我们已经重写了基本WebHttpBehavior以添加其他功能,但这导致我们失去了基本功能,其中包括执行模板路由的功能。如果你查看我们卡洛斯的文章,我链接到他,他甚至说他们不包括在样本中,因为它是如此困难。我正在寻找如何在重写的方法/类中执行此操作的参考或示例。@BrentPabst,来自Carlos“blog”。。。“在新的WCF Web API(您可以在Codeplex网站上预览位)中,实现这种场景要简单得多,而其他功能(如UriTemplate)将在插入新的格式化程序后继续工作。”。。。你读过吗?如果尸体真的被传了进来,例如在一个帖子或便笺中,这将不起作用。
public Person GetPerson(string lastName)
{
    return new Person
    {
        FirstName = "First",
        LastName = lastName, // Return the argument.
        BirthDate = new DateTime(1993, 4, 17, 2, 51, 37, 47, DateTimeKind.Local),
        Id = 0,
        Pets = new List<Pet>
        {
            new Pet { Name= "Generic Pet 1", Color = "Beige", Id = 0, Markings = "Some markings" },
            new Pet { Name= "Generic Pet 2", Color = "Gold", Id = 0, Markings = "Other markings" },
        },
    };
}
[Request]
SendRequest(baseAddress + "/json/GetPersonByName?l=smith", "GET", null, null);

[Response]
{
"FirstName": "First",
"LastName": "smith",
"BirthDate": "1993-04-17T02:51:37.047-04:00",
"Pets": [
{...},
{...}
}