English 中文(简体)
Access WCF service on same server
原标题:
  • 时间:2009-11-09 20:24:19
  •  标签:
  • .net
  • wcf

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.

最佳回答

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.

问题回答

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.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签