gpt4 book ai didi

在多种方法中找到的 WCF Web API UriTemplate 元素

转载 作者:行者123 更新时间:2023-12-04 15:47:59 27 4
gpt4 key购买 nike

假设我正在使用新的 WCF Web API 来构建 RESTful 服务,并且在我的服务中,我有一部分 URI 将描述目标资源,但用于(几乎)契约(Contract)的所有方法。例如,如果我有一个处理电子商务的用户服务,它可能如下所示:

[ServiceContract]
public class MyUserService
{
private MyUserRepository _UserRepo;
private MyOrganizationRepository _OrgRepo;

[WebGet (UriTemplate = "{OrganizationName}/Users")]
public IEnumerable<User> GetUsers (string OrganizationName)
{
IEnumerable<User> Users = null;
var Organization = _OrgRepo.GetOrgByName (OrganizationName);

if (Organization != null)
{
Users = Organization.GetUsers ();
}
else
{
throw new WebFaultException<string> ("Organization not found.", HttpStatusCode.NotFound);
}

return Users;
}

[WebInvoke (UriTemplate = "{OrganizationName}/Users", /*yada...yada...yada*/)]
public User AddNewUser (string OrganizationName, User User)
{
// Find the organization, like above, and throw if null.
}
}

如果我必须不断加载组织并测试 null,这将使我的代码陷入困境并且不是很 DRY。 (很想拼出 DRY...)我想要做的是在 MyUserService 类中加载一个属性,当 {OrganizationName} 包含在 URI 中时填充该属性,否则抛出 WebFaultException。因为这是 URI 的一部分,所以最好的方法是什么?

编辑:

对于那些可能感兴趣的人,这里是我想出的 HttpOperationHandler 的一个例子。似乎没有很多信息可以涵盖这一点。我还找到了有关 Processors 的更多信息它将随 WCF Web Api 套件一起提供,看起来他们将更好地处理此类事情,以取代 HttpOperationHandlers,而且似乎它们可能更易于使用。 (这只是一个例子,用于介绍一些我发现很难找到的东西。我在我的申请中写得有点不同。)
using Microsoft.ApplicationServer.Http.Dispatcher;   // For HttpOperationHandler
using Microsoft.ApplicationServer.Http.Description; // For HttpOperationHandlerFactory

public class OrganizationHandler : HttpOperationHandler<string, Organization>
{
private Repository<Organization> _OrganizationRepository;

public OrganizationHandler (UnitOfWork Work)
: base ("OrganizationName")
{
_OrganizationRepository = Work.Organizations;
}

public override Organization OnHandle (string OrganizationName)
{
var Result = _OrganizationRepository
.Get (O => O.UrlSafeName.Equals (OrganizationName,
StringComparison.InvariantCultureIgnoreCase));

if (Result == null)
{
throw new WebFaultException<string> ("Organization not found.");
}

return Result;
}
}

public class OrganizationHandlerFactory : HttpOperationHandlerFactory
{
private UnitOfWork _Work;

public OrganizationHandlerFactory (UnitOfWork Work)
{
_Work = Work;
}

protected override Collection<HttpOperationHandler> OnCreateRequestHandlers
(ServiceEndpoint endpoint, HttpOperationDescription operation)
{
var Collection = base.OnCreateRequestHandlers (endpoint, operation);

if (operation.InputParameters.Any (IP => IP.Type.Equals (typeof (Organization))))
{
var Binding = endpoint.Binding as HttpBinding;

if (Binding != null)
{
Collection.Add (new OrganizationHandler (_Work));
}
}

return Collection;
}
}

然后将其连接到 Global.asax(我使用 Ninject 进行 IoC):
// Add this reference to get the MapServiceRoute<T> extension
using Microsoft.ApplicationServer.Http.Activation;

public class Global : HttpApplication
{
protected void Application_Start (object sender, EventArgs e)
{
var Kernel = BuildKernel ();

var Config = HttpHostConfiguration.Create ()
.SetOperationHandlerFactory
(Kernel.Get (typeof (OrganizationHandlerFactory)) as OrganizationHandlerFactory)
.SetResourceFactory (new NinjectResourceFactory (Kernel));


RouteTable.Routes.MapServiceRoute<OrganizationService> ("Organizations", Config);
}

protected IKernel BuildKernel ()
{
IKernel Kernel = new Ninject.StandardKernel ();

// Load up the Kernel

return Kernel;
}
}

public class NinjectResourceFactory : IResourceFactory
{
private readonly IKernel _Kernel;

public NinjectResourceFactory (IKernel Kernel)
{
_Kernel = Kernel;
}

public object GetInstance (Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
{
return Resolve (serviceType);
}

public void ReleaseInstance (InstanceContext instanceContext, object service)
{
throw new NotImplementedException ();
}

private object Resolve (Type type)
{
return _Kernel.Get (type);
}
}

这是在我的服务中:
[ServiceContract]
[ServiceBehavior (InstanceContextMode = InstanceContextMode.PerCall)]
public class OrganizationService
{
[WebGet (UriTemplate = "{OrganizationName}/Products")]
public IEnumerable<Product> GetProducts (Organization Organization)
{
return Organization.Products;
}
}

最佳答案

这正是 OperationHandlers 的用途。您创建一个 OperationHandler,它将 URI 参数转换为一个强类型对象,您可以将其作为操作的参数接受。

关于在多种方法中找到的 WCF Web API UriTemplate 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6066204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com