是否可以从同一项目使用 WCF 服务?

发布于 2024-12-06 04:06:09 字数 273 浏览 0 评论 0原文

我有一个 ASP.NET MVC 3 应用程序,它在同一项目中使用 WCF 服务。理想情况下,我想使用 jQuery 调用此服务。然而,我似乎无法全神贯注于我正在做的事情。我还应该在配置中创建端点吗?现在我收到以下异常:

此服务的安全设置需要“匿名”身份验证,但托管此服务的 IIS 应用程序未启用该身份验证。

我可以为 IIS 启用匿名身份验证,但我更喜欢使用 Windows。当我设置绑定配置时,由于没有端点,我不确定将该配置添加到哪里。

I have an ASP.NET MVC 3 application that uses a WCF service within the same project. Ideally I'd like to call out to this service using jQuery. However, I cannot seem to wrap my head around what I'm doing. Should I still create an endpoint in the configuration? Right now I receive the following exception:

Security settings for this service require 'Anonymous' Authentication but it is not enabled for the IIS application that hosts this service.

I can enable anonymous authentication for IIS but I'd prefer to use Windows. When I setup a binding configuration, since there is no endpoint, I'm not sure where to add that configuration to.

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

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

发布评论

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

评论(2

感情洁癖 2024-12-13 04:06:09

如果您希望能够访问 WCF 服务,通常需要设置一个端点。另一种方法是使用 InProcFactory 类托管“In-Proc”服务,该类是 Juval Löwy 的 ServiceModelEx 库的一部分,可从 他的网站的下载页面(注册是需要下载它,只需搜索“ServiceModelEx”并单击链接)。这种方法看起来像:

IMyService proxy = InProcFactory.CreateInstance<MyServiceClass, IMyService>();
proxy.MyMethod();

如果您不需要进行任何自定义配置,这会减少配置;但是,一旦达到默认配置的边界,您就需要返回使用已配置的端点,或者寻找一种以编程方式更新服务配置的方法。

If you want to be able to reach your WCF service, you will generally need to setup an endpoint. An alternative approach would be to host your service "In-Proc" using the InProcFactory clas, which is part of the ServiceModelEx library from Juval Löwy available from the downloads page of his website (registration is required to download it, just search for "ServiceModelEx" and click the link). That approach would look like:

IMyService proxy = InProcFactory.CreateInstance<MyServiceClass, IMyService>();
proxy.MyMethod();

This reduces the configuration if you don't need to do any custom configuration; however, as soon as you hit a boundary with the default configuration, you'll either need to go back to using a configured endpoint, or looking for a way to programmatically update your service's configuration.

月野兔 2024-12-13 04:06:09

您将需要一个端点,但与所有 WCF 端点一样,它不一定需要在配置文件中定义 - 您可以自由地在代码中定义它。

由于您已经在 Web 项目中,最简单的解决方案是在 IIS 中托管 WCF 服务。这可以通过配置文件轻松完成,并且在 .NET 4 中,大多数配置都是默认的(比 3.5 简单得多)。

定义服务后,您需要实例化通道或客户端。您可以使用 svcutil 工具生成代理(使用“添加新服务引用...”向导),或者仅创建一个 ChannelFactory

var factory = new ChannelFactory<MyService>(typeof(MyService).FullName);
MyService channel = factory.CreateChannel();

// use the channel as you would a normal instance of the class
var result = channel.MyOperation("hello world");

再次,此模式将检索来自 web.config 文件的配置。由于您的项目既是服务也是客户端,因此您将需要这两个部分。这不是完整的配置,但应该可以让您了解......

<system.serviceModel>
  <services>
    <service name="MyProject.MyService">
      <endpoint binding="basicHttpBinding" 
                contract="MyProject.IMyService" />
    </service>
  </services>
  <client>
    <endpoint name="MyProject.MyService" 
              address="http://localhost" 
              binding="basicHttpBinding" 
              contract="MyProject.IMyService" />
  </client>    
</system.serviceModel>

You'll need an endpoint, but as with all WCF endpoints it doesn't necessarily need to be defined in the config file - you're free to define it in code.

As you're already in a web project, your simplest solution will be to host the WCF service in IIS. This works very easily with a config file, and in .NET 4 most of the configuration is defaulted (a lot simpler than 3.5)

Once your service is defined you need to instantiate a channel or a client. You can use the svcutil tool to generate a proxy (using the 'Add New Service Reference...' wizard), or just create a ChannelFactory

var factory = new ChannelFactory<MyService>(typeof(MyService).FullName);
MyService channel = factory.CreateChannel();

// use the channel as you would a normal instance of the class
var result = channel.MyOperation("hello world");

Again, this pattern will retrieve configuration from your web.config file. Because your project is both the service and the client, you'll need both sections. This isn't a complete configuration but should give you the idea...

<system.serviceModel>
  <services>
    <service name="MyProject.MyService">
      <endpoint binding="basicHttpBinding" 
                contract="MyProject.IMyService" />
    </service>
  </services>
  <client>
    <endpoint name="MyProject.MyService" 
              address="http://localhost" 
              binding="basicHttpBinding" 
              contract="MyProject.IMyService" />
  </client>    
</system.serviceModel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文