WCF WebApi';s自我托管不接受PUT动词

WCF WebApi';s自我托管不接受PUT动词,wcf,wcf-web-api,Wcf,Wcf Web Api,我使用了一个HTTP驱动的API,它使用put动词。当托管在托管在IIS Express上的MVC3项目中时,一切都按设计工作 然而,当我进行单元测试时,我偶尔会想测试传输方面,而不仅仅是针对我自己的资源。我的单元测试失败,405-MethodNotAllowed。同样,IIS中承载的服务也完全相同(我在配置文件中启用了PUT和DELETE谓词) 我如何让测试中使用的“自托管”服务也接受这些动词? 几乎相同的“get”测试可以工作,所以我不认为下面的概念有错。。希望 [Test] public

我使用了一个HTTP驱动的API,它使用put动词。当托管在托管在IIS Express上的MVC3项目中时,一切都按设计工作

然而,当我进行单元测试时,我偶尔会想测试传输方面,而不仅仅是针对我自己的资源。我的单元测试失败,405-MethodNotAllowed。同样,IIS中承载的服务也完全相同(我在配置文件中启用了PUT和DELETE谓词)

我如何让测试中使用的“自托管”服务也接受这些动词?

几乎相同的“get”测试可以工作,所以我不认为下面的概念有错。。希望

[Test]
public void PutNewMachine()
{
    // Create new record to add
    var machine = new Machine
                      {
                          ID = 1,
                          Name = "One",
                          Description = "Machine #1",
                          Location = 1
                      };

    using (var client = new HttpClient())
    {
        using (var request = new HttpRequestMessage(
                                         HttpMethod.Put, 
                                         HOST + "/1"))
        {
            request.Content = new ObjectContent<Machine>(machine);

            using (var response = client.Send(request))
            {
                Assert.AreEqual(
                    HttpStatusCode.Created,
                    response.StatusCode,
                    "New record put should have been acknowledged "
                                    + "with a status code of 'Created'");
            }
        }
    }
}
[测试]
public void PutNewMachine()
{
//创建要添加的新记录
var机器=新机器
{
ID=1,
Name=“一”,
Description=“机器#1”,
位置=1
};
使用(var client=new HttpClient())
{
使用(var request=newhttprequestmessage)(
HttpMethod.Put,
主机+“/1”))
{
request.Content=新对象内容(机器);
使用(var响应=client.Send(请求))
{
断言你是平等的(
HttpStatusCode.Created,
response.StatusCode,
“应已确认新的记录”
+“状态代码为‘已创建’”;
}
}
}
}
在测试的设置中,我使用以下Autofac代码准备端点(这同样适用于“Get”):

var builder=newcontainerbuilder();
建设者
.Register(c=>new FakeDatabase())
.As()
.SingleInstance();
建设者
.Register(c=>newgenerirepository(c.Resolve()))
.As();
建设者
.Register(c=>new MachineService(c.Resolve()))
.As();
Container=builder.Build();
Scope=Container.BeginLifetimeScope();
主机=新的HttpServiceHost(类型为(MachineService),主机);
host.AddDependencyInjectionBehavior(容器);
host.Open();
我的服务在以下界面中定义:

[ServiceContract]
public interface IResourceService<in TKey, TResource>
{
    [WebGet(UriTemplate = "{key}")]
    TResource Get(TKey key);

    [WebInvoke(Method = "PUT", UriTemplate = "{key}")]
    TResource Put(TKey key, TResource resource);

    [WebInvoke(Method = "POST")]
    TResource Post(TResource resource);

    [WebInvoke(Method = "DELETE", UriTemplate = "{key}")]
    void Delete(TKey key);
}
[服务合同]
公共接口IResourceService
{
[WebGet(UriTemplate=“{key}”)]
TResource-Get(TKey);
[WebInvoke(Method=“PUT”,UriTemplate=“{key}”)]
TResource Put(TKey key,TResource资源);
[WebInvoke(Method=“POST”)]
TResource Post(TResource资源);
[WebInvoke(Method=“DELETE”,UriTemplate=“{key}”)]
作废删除(TKey);
}
因此,例如,如果我有一个MachineService,它将实现接口(既
类MachineService:IResourceService
..:IResourceService
都已试运行-Get=OK,Put=Nothing

编辑:我似乎在InternalServerError和MethodNotAllowed错误之间跳跃-仅在使用自托管时。我已确保我作为用户有权打开端口(Win7+非管理员),但结果加上我对端口的选择似乎可以预测Get的功能。“Post”似乎也有类似的问题!:-(

EDIT2:界面现在已更改为可正常工作的界面

[ServiceContract]
public interface IResourceService<in TKey, TResource>
{
    [WebGet(UriTemplate = "{key}")]
    TResource Get(TKey key);

    [WebInvoke(Method = "PUT", UriTemplate = "{key}")]
    TResource Put(HttpRequestMessage<TResource> resourceRequest, TKey key);

    [WebInvoke(Method = "POST", UriTemplate = "{key}")]
    TResource Post(HttpRequestMessage<TResource> resourceRequest, TKey key);

    [WebInvoke(Method = "DELETE", UriTemplate = "{key}")]
    void Delete(TKey key);
}
[服务合同]
公共接口IResourceService
{
[WebGet(UriTemplate=“{key}”)]
TResource-Get(TKey);
[WebInvoke(Method=“PUT”,UriTemplate=“{key}”)]
TResource Put(HttpRequestMessage resourceRequest,TKey);
[WebInvoke(Method=“POST”,UriTemplate=“{key}”)]
TResource Post(HttpRequestMessage resourceRequest,TKey);
[WebInvoke(Method=“DELETE”,UriTemplate=“{key}”)]
作废删除(TKey);
}

当我将方法签名更改为接受HttpRequestMessage请求而不是T本身时,执行PUT或POST对我有效。

您是否使用非通用服务接口进行了尝试?尝试使用HttpConfigurableServiceHost而不是HttpServiceHost。是的,我从非通用开始,仍然有一个显示相同p的测试服务问题。我不知道为什么我会认为这可能会起作用,相反,我现在正在接受HttpRequest消息。我现在唯一的问题是,我在测试中发出的“请求”似乎格式不正确-但至少我越来越接近了!@Ray:也许因为,至少,它应该起作用:-)它确实在web应用程序中工作。我已经报告了这个问题
[ServiceContract]
public interface IResourceService<in TKey, TResource>
{
    [WebGet(UriTemplate = "{key}")]
    TResource Get(TKey key);

    [WebInvoke(Method = "PUT", UriTemplate = "{key}")]
    TResource Put(HttpRequestMessage<TResource> resourceRequest, TKey key);

    [WebInvoke(Method = "POST", UriTemplate = "{key}")]
    TResource Post(HttpRequestMessage<TResource> resourceRequest, TKey key);

    [WebInvoke(Method = "DELETE", UriTemplate = "{key}")]
    void Delete(TKey key);
}