是否可以从同一项目使用 WCF 服务?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望能够访问 WCF 服务,通常需要设置一个端点。另一种方法是使用
InProcFactory
类托管“In-Proc”服务,该类是 Juval Löwy 的ServiceModelEx
库的一部分,可从 他的网站的下载页面(注册是需要下载它,只需搜索“ServiceModelEx”并单击链接)。这种方法看起来像:如果您不需要进行任何自定义配置,这会减少配置;但是,一旦达到默认配置的边界,您就需要返回使用已配置的端点,或者寻找一种以编程方式更新服务配置的方法。
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 theServiceModelEx
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: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.
您将需要一个端点,但与所有 WCF 端点一样,它不一定需要在配置文件中定义 - 您可以自由地在代码中定义它。
由于您已经在 Web 项目中,最简单的解决方案是在 IIS 中托管 WCF 服务。这可以通过配置文件轻松完成,并且在 .NET 4 中,大多数配置都是默认的(比 3.5 简单得多)。
定义服务后,您需要实例化通道或客户端。您可以使用 svcutil 工具生成代理(使用“添加新服务引用...”向导),或者仅创建一个 ChannelFactory
再次,此模式将检索来自
web.config
文件的配置。由于您的项目既是服务也是客户端,因此您将需要这两个部分。这不是完整的配置,但应该可以让您了解......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 aChannelFactory
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...