如何在不添加 Web 引用的情况下访问项目 B 中的代码

本文关键字:访问 情况下 项目 代码 引用 添加 Web | 更新日期: 2023-09-27 18:34:58

IDE: VS 2010, C# .net WInfomrs

我有两个项目A和B
答:在一个项目中,我正在做UI工作
B:B项目负责数据库功能、逻辑等

我有一个 Web 服务 W,为了使用该服务,我在项目 A 中添加了该 Web 引用,因此 App.config 位于项目 A 中。

现在我想在项目 B 中访问

和使用 WebService,您能否建议有没有办法在不添加 Web 服务引用的情况下在项目 B 中访问它。.

基本上我想将strXMLString,strFileName参数传递给该Web服务

我想在项目B中写的CODE:

 MyServiceSoapClient sc = new _MyServiceSoapClient ();
 sc.ReceiveXMLByContent("<tag1>text</tag1>", "myTest.xml");

我能够在项目 A 中编写此代码,但您能否建议如何在项目 B 中实现相同的目标。

我试图通过发送 HttpPost 来克服这个问题,但收到错误 500

/*
 //stack trace:  
   at System.Net.HttpWebRequest.GetResponse()
   at UseWebServiceWithoutReference.Form1.button11_Click(Object sender, EventArgs e) in  
    C:'Users    'Yogesh'documents'visual studio 2010'Projects'UseWebServiceWithoutReference'UseWebServiceWithoutReference'Form1.cs:line 369
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at UseWebServiceWithoutReference.Program.Main() in c:'users'Yogesh'documents'visual studio 2010'Projects'UseWebServiceWithoutReference'UseWebServiceWithoutReference'Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart() 

网络马托德:

    [WebMethod]
    public string ReceiveXMLByContent(string strXMLData, string strXMLFileName)
    {
        XMLReceiver.Receive(strXMLData, strXMLFileName);
        return "Worked";
    }  

客户端代码

private void button11_Click(object sender, EventArgs e)
    {
        string data = string.Format("strXMLData={0}&strXMLFileName={1}", "<tag1>text</tag1>", "myTest.xml");
        byte[] dataStream = Encoding.UTF8.GetBytes(data);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:50221/MyWebService.asmx/ReceiveXMLByContent");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = dataStream.Length;
        Stream newStream = request.GetRequestStream();
        newStream.Write(dataStream, 0, dataStream.Length);
        newStream.Close();
        var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
        string dataReturn = reader.ReadToEnd();
        Console.WriteLine(dataReturn);
        Console.ReadLine();
    }

你能告诉我错误 500 背后的问题是什么,当我传递简单文本而不是

 string data = string.Format("strXMLData={0}&strXMLFileName={1}", "<tag1>text</tag1>", "myTest.xml");  //Not working error 500  
string data = string.Format("strXMLData={0}&strXMLFileName={1}", "myTestString", "myTest.xml");//Working ..

如何在不添加 Web 引用的情况下访问项目 B 中的代码

如果我假设您的 Web 服务方法看起来像这样.

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string TestMethod(string data ,string fileName)
        {
            return "Hello World";
        }
    }

然后,您可以使用 HttpWebRequest 调用 Web 服务,而无需任何 Web 引用。

 string data = string.Format("data={0}&fileName={1}", "<tag1>text</tag1>", "myTest.xml");
            byte[] dataStream = Encoding.UTF8.GetBytes(data);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:63169/Service1.asmx/TestMethod");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = dataStream.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(dataStream, 0, dataStream.Length);
            newStream.Close();
            var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
            string dataReturn = reader.ReadToEnd();
            Console.WriteLine(dataReturn);
            Console.ReadLine();

哇 - 停在那里...

其中一位评论者@Avijit问了一个非常好的问题——你为什么要避免网络引用?您没有证明该要求的合理性,现在正在逆流而上,寻找巧妙的解决方案,使您能够在不使用 WebReference 的情况下调用方法。

我认为,在进一步讨论之前,您需要问问自己,您迫切希望避免的Web参考是什么。你真正的问题,至少从我坐的位置来看,不是"如何在没有 Web 引用的情况下调用 Web 服务?",而是"我如何绕过 Web 引用的烦人属性 x...">

最后,

我创建了自己的编码器和解码器:我在传递服务器之前对 xml 进行了编码,并在服务器上解码回

 private string MyHttpDecoder(string encodedXML)
    {
        //  string decoded = encodeXML.Replace("''u003c", "<").Replace("''u003e", ">").Replace("'"","");
        string decoded = encodedXML.Replace("66", "<").Replace("67", ">").Replace("68", "'"");
        return decoded;
    }  
private string MyHttpEncoder(string XML)
    {   
        string enc = XML.Replace("<", "66").Replace(">", "67").Replace("'"", "68");
        return enc;
    }  

这个工作魅力。