开发者

Access WCF service on same server

开发者 https://www.devze.com 2022-12-11 03:20 出处:网络
I have 开发者_开发技巧a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...b

I have 开发者_开发技巧a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems sloppy and redundant; especially when the service and website are the same application.

-- update

The goal is to create a notification system via Silverlight and WCF. When a user creates a game, they'll need to wait for a player. When another player decides to join a game via the game list page, which is just standard HTML, the creator must be notified that someone wants to play. User pushes the "Join" game button, server does a page postback, and talks to the WCF service. WCF then pushes the message to the silverlight interface of the game creator.

Now I could just create a web reference to my own application, but I'm looking to bypass that step, since they're both on the same server; same app for that matter.


I agree with David; if what you want is to call the service without going through WCF, you can simply instantiate the service class.

On the other hand, if you want the WCF service to be able to access the HTTPContext object and have full access to the web user's session, then you add the AspNetCompatibilityRequirements attribute on your service class (not interface):

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService: IMyService
{
    public string DoWork(string myInput)
    {
        HTTPContext context = HTTPContext.Current;
        ...
        return myInput;
    }
}

Whether this is good or bad or close coupling or whatever is really dependent on your usage and availability of the service.


Can you define "current operations context?"

If you just want to use the service from within the same project or site, you would just instantiate the service like you would any other class:

public class MyWCF : IMyWCF
{
  public void DoWork()
  {
    /// do something
  }
}

elsewhere...

IMyWcf wcf = new MyWcf();
wcf.DoWork();

The difference between using the service here or in another app is that you don't use the proxy/client object generated when you add a service reference.


You need to provide a bit more context and architectural detail. Primarily...from where, exactly, do you need the operation context? If you are trying to utilize the operation context outside of the scope of the web service, then I would say you are creating a VERY BAD coupling between your application and an infrastructural and contextual detail that your application has absolutely NO business knowing about.

Again, you need to clarify your question so I can provide a better answer.


After much research and a clearer understanding of WCF I've now found my answer. I was using DuplexService binding (active session/instance retained on both client/server). Accessing Duplex session instances is not possible through a standard aspx web page. You must have a client (silverlight or windows app) that can maintain an active session to the server, in case the server pushes any messages to the client.

0

精彩评论

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