Override Default Constructor of Partial Class with Another Partial Class

前端 未结 12 1869
时光取名叫无心
时光取名叫无心 2020-12-28 12:59

I don\'t think this is possible, but if is then I need it :)

I have a auto-generated proxy file from the wsdl.exe command line tool by Visual Studio 2008.

Th

12条回答
  •  梦谈多话
    2020-12-28 13:24

    The problem that the OP has got is that the web reference proxy doesn't generate any partial methods that you can use to intercept the constructor.

    I ran into the same problem, and I can't just upgrade to WCF because the web service that I'm targetting doesn't support it.

    I didn't want to manually amend the autogenerated code because it'll get flattened if anyone ever invokes the code generation.

    I tackled the problem from a different angle. I knew my initialization needed doing before a request, it didn't really need to be done at construction time, so I just overrode the GetWebRequest method like so.

    protected override WebRequest GetWebRequest(Uri uri)
    {
        //only perform the initialization once
        if (!hasBeenInitialized)
        {
            Initialize();
        }
    
        return base.GetWebRequest(uri);
    }
    
    bool hasBeenInitialized = false;
    
    private void Initialize()
    {
        //do your initialization here...
    
        hasBeenInitialized = true;
    }
    

    This is a nice solution because it doesn't involve hacking the auto generated code, and it fits the OP's exact use case of performing initialization login for a SoapHttpClientProtocol auto generated proxy.

提交回复
热议问题