WCFacility 和 WVF 4.0 REST

发布于 2024-11-06 02:03:58 字数 116 浏览 0 评论 0原文

如何将 Windsor-Castle WCFFacility 与 WCF 4.0 REST 服务结合使用?

当您不再拥有 .svc 文件时,如何链接到工厂?

蒂亚·

索伦

How do you use the Windsor-Castle WCFFacility with the WCF 4.0 REST services ?

How you you make the link to the factory, when you don't have the .svc file anymore?

TIA

Søren

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-11-13 02:03:58

使用 Windsor 3.0 这非常简单(如果我正确理解了你的问题,如果我遗漏了一些东西,我很抱歉)。

向您展示的最简单的方法是创建一个控制台应用程序并确保您引用:

  • Castle.Core
  • Castle.Windsor
  • Castle.Facilities.WcfIntegration
  • System.ServiceModel
  • System.ServiceModel.Web
  • System.Runtime.Serialization

现在定义一个 RESTful 服务像这样:

[DataContract]
public class Frob
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Fribble { get; set; }
}   

[ServiceContract]
public interface IFrobService
{
    [OperationContract]
    [WebGet(UriTemplate = "/")]
    IEnumerable<Frob> GetAllFrobs();

    [OperationContract]
    [WebGet(UriTemplate = "/{name}")]
    Frob GetFrobByName(string name);
}

public class FrobService : IFrobService
{
    private readonly List<Frob> _frobs
        = new List<Frob>
              {
                  new Frob {Name = "Foob", Fribble = "Soop"},
                  new Frob {Name = "Hoob", Fribble = "Soop"},
                  new Frob {Name = "Doob", Fribble = "Noop"}
              };

    public IEnumerable<Frob> GetAllFrobs()
    {
        return _frobs;
    }

    public Frob GetFrobByName(string name)
    {
        return _frobs
            .FirstOrDefault(f =>
                            f.Name.Equals(name,
                                          StringComparison.OrdinalIgnoreCase));
    }
}

现在您可以像这样将该服务连接到温莎容器中(因为它是一个控制台应用程序,我将只向您展示主要方法):

public static class Program
{
    static void Main()
    {            
        var container = new WindsorContainer();

        container
            .AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
            .Register(Component.For<IFrobService>()
                          .ImplementedBy<FrobService>()
                          .AsWcfService(new RestServiceModel("http://localhost/frobs")));

        Console.ReadKey();
    }
}

这是由 Castle Windsor 托管的 WCF REST 服务。

将浏览器指向“http://localhost/frobs”将获得所有 frobs,将浏览器指向“http://localhost/frobs/Doob”将获得名为 Doob 的 frob,您将获得主意...

Using Windsor 3.0 this is pretty straightforward (if I have understood your question correctly, my apologies if I am missing something).

The simplest thing to do to show you is to create a console application and make sure you are referencing:

  • Castle.Core
  • Castle.Windsor
  • Castle.Facilities.WcfIntegration
  • System.ServiceModel
  • System.ServiceModel.Web
  • System.Runtime.Serialization

Now define a RESTful service like this:

[DataContract]
public class Frob
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Fribble { get; set; }
}   

[ServiceContract]
public interface IFrobService
{
    [OperationContract]
    [WebGet(UriTemplate = "/")]
    IEnumerable<Frob> GetAllFrobs();

    [OperationContract]
    [WebGet(UriTemplate = "/{name}")]
    Frob GetFrobByName(string name);
}

public class FrobService : IFrobService
{
    private readonly List<Frob> _frobs
        = new List<Frob>
              {
                  new Frob {Name = "Foob", Fribble = "Soop"},
                  new Frob {Name = "Hoob", Fribble = "Soop"},
                  new Frob {Name = "Doob", Fribble = "Noop"}
              };

    public IEnumerable<Frob> GetAllFrobs()
    {
        return _frobs;
    }

    public Frob GetFrobByName(string name)
    {
        return _frobs
            .FirstOrDefault(f =>
                            f.Name.Equals(name,
                                          StringComparison.OrdinalIgnoreCase));
    }
}

Now you have that you can hook up that service into the windsor container like so (and since it is a console application, I will just show you the main method):

public static class Program
{
    static void Main()
    {            
        var container = new WindsorContainer();

        container
            .AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
            .Register(Component.For<IFrobService>()
                          .ImplementedBy<FrobService>()
                          .AsWcfService(new RestServiceModel("http://localhost/frobs")));

        Console.ReadKey();
    }
}

And that is a WCF REST service hosted by Castle Windsor.

Pointing a browser at: "http://localhost/frobs" will get you all the frobs and pointing a browser at, say, "http://localhost/frobs/Doob" will get you the frob called Doob, you get the idea...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文