开发者

Is it possible to consume WCF service from same project?

开发者 https://www.devze.com 2023-04-06 01:25 出处:网络
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

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 host开发者_如何学Cs 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.


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号