如何在安装文件中使用webservice[英] how to use webservice in setup file

本文是小编为大家收集整理的关于如何在安装文件中使用webservice的处理方法,想解了如何在安装文件中使用webservice的问题怎么解决?如何在安装文件中使用webservice问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我必须制作安装文件,所以我从安全的角度制作了 Web 服务.现在我必须在安装文件中使用该 Web 服务.请给我解决方案.

推荐答案

使用 Web 服务访问器类
我也是一样的
public class WebServiceAccessor
{
    private Assembly _ass = null;
    // assembly of the web service proxy

    private string _protocolName = "Soap";
    // communication protocol

    private string _srcWSProxy = string.Empty;
    // source text (.cs) 

    //

    public Assembly Assembly { get { return _ass; } }
    public string ProtocolName
    { get { return _protocolName; } set { _protocolName = value; } }
    public string SrcWSProxy { get { return _srcWSProxy; } }

    public WebServiceAccessor()
    {
    }
    public WebServiceAccessor(string wsdlSourceName)
    {
        AssemblyFromWsdl(GetWsdl(wsdlSourceName));
    }
    // Get the wsdl text from specified source

    public string WsdlFromUrl(string url)
    {
        WebRequest req = WebRequest.Create(url);
        WebResponse result = req.GetResponse();
        Stream ReceiveStream = result.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
        StreamReader sr = new StreamReader(ReceiveStream, encode);
        string strWsdl = sr.ReadToEnd();
        return strWsdl;
    }
    public string GetWsdl(string source)
    {
        if (source.StartsWith("<?xml version") == true)
        {
            return source;                // this can be a wsdl string

        }
        else
            if (source.StartsWith("http://") == true)
            {
                return WsdlFromUrl(source);     // this is a url address

            }

        return WsdlFromFile(source);    // try to get from the file system

    }
    public string WsdlFromFile(string fileFullPathName)
    {
        FileInfo fi = new FileInfo(fileFullPathName);
        if (fi.Extension == "wsdl")
        {
            FileStream fs = new FileStream(fileFullPathName, FileMode.Open,
                FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            char[] buffer = new char[(int)fs.Length];
            sr.ReadBlock(buffer, 0, (int)fs.Length);
            return new string(buffer);
        }

        throw new Exception("This is no a wsdl file");
    }
    // make assembly for specified wsdl text

    public Assembly AssemblyFromWsdl(string strWsdl)
    {
        // Xml text reader

        StringReader wsdlStringReader = new StringReader(strWsdl);
        XmlTextReader tr = new XmlTextReader(wsdlStringReader);
        ServiceDescription sd = ServiceDescription.Read(tr);
        tr.Close();

        // WSDL service description importer 

        CodeNamespace cns = new CodeNamespace("Sanjay.WebServiceAccessor");
        ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
        sdi.AddServiceDescription(sd, null, null);
        sdi.ProtocolName = _protocolName;
        sdi.Import(cns, null);

        // source code generation

        CSharpCodeProvider cscp = new CSharpCodeProvider();
        ICodeGenerator icg = cscp.CreateGenerator();
        StringBuilder srcStringBuilder = new StringBuilder();
        StringWriter sw = new StringWriter(srcStringBuilder);
        icg.GenerateCodeFromNamespace(cns, sw, null);
        _srcWSProxy = srcStringBuilder.ToString();
        sw.Close();

        // assembly compilation.

        CompilerParameters cp = new CompilerParameters();
        cp.ReferencedAssemblies.Add("System.dll");
        cp.ReferencedAssemblies.Add("System.Xml.dll");
        cp.ReferencedAssemblies.Add("System.Web.Services.dll");
        cp.ReferencedAssemblies.Add("System.Data.dll");
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
        cp.IncludeDebugInformation = false;
        ICodeCompiler icc = cscp.CreateCompiler();
        CompilerResults cr = icc.CompileAssemblyFromSource(cp, _srcWSProxy);
        if (cr.Errors.Count > 0)
            throw new Exception(string.Format("Build failed: {0} errors",
                cr.Errors.Count));

        return _ass = cr.CompiledAssembly;
    }
    // Create instance of the web service proxy

    public object CreateInstance(string objTypeName)
    {
        Type t = _ass.GetType("Sanjay.WebServiceAccessor" + "." + objTypeName);
        return Activator.CreateInstance(t);
    }
    // invoke method on the obj

    public object Invoke(object obj, string methodName, params object[] args)
    {
        MethodInfo mi = obj.GetType().GetMethod(methodName);
        return mi.Invoke(obj, args);
    }
}

用作
WebServiceAccessor objWebServiceAccessor=new WebServiceAccessor("ServiveAddrr.asmx");
objWebServiceAccessor.Invoke("ServiveMethod",params);

本文地址:https://www.itbaoku.cn/post/1356006.html

相关标签/搜索