简体   繁体   中英

wcf interface: why doesn't it 'just' go to the methode but to the whole class

I have WCF service implemented and the connection works just fine. I use BasicHttpBinding .

[ServiceContract]
public interface IScannerInput
{
    [OperationContract]
    string ScannedPRX(string barcode, string user, int color);
}

public class ProcessPRX : IScannerInput
{
    ProcessClass c = new ProcessClass(); // every time a call ScannedPRX() this class is made again

    public string ScannedPRX(string barcode, string user, int color)
    {
        c.PrxScannedInput(barcode, user, color);
        return "Bussy processing: " + barcode;
    }
}

In a normal class I can just make ProcessClass c one time. But now it is made again and again every time a call the methode ScannedPRX() . What am I doing wrong? It is not just going to the methode but to the whole class.

There is three ways of instantiating WCF service object:

  • PerCall: A new InstanceContext (and therefore service object) is created for each client request.

  • PerSession: A new InstanceContext (and therefore service object) is created for each new client session and maintained for the lifetime of that session (this requires a binding that supports sessions).

  • Single: A single InstanceContext (and therefore service object) handles all client requests for the lifetime of the application.

PerCall is default one, and that is what you are having.

If you want other behaviour read article below.

http://msdn.microsoft.com/en-us/library/ms731193.aspx

Take into account concurrent request that are made to your service, as if you are choosing for instance Single instantiation mode, you need to take care on your own that all your methods are thread safe.

Because the default instancing behavior for WCF services is to create a new instance for every call. You generally want this to avoid sharing state between different callers of your service or multiple invocations by the same client. Unless ProcessClass is expensive to create or you need to maintain state between calls, I would stick with this model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM