将所有帖子保存到XML中的.NET Web Service到DB

时间:2010-06-01 02:23:29

标签: c# web-services asmx

我确信这是一个简单的问题,但我找不到解决方案。我想保存发布到我的.NET Web服务的XML文件。我确信在调用服务时它只是一个简单的方法调用,但我找不到它。我想保存发布到服务的完整XML

非常感谢任何帮助。提前谢谢。

4 个答案:

答案 0 :(得分:2)

感谢您的帮助,但我在http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension.aspx

找到了我想要的内容

为了帮助其他新手来通过WebService实现这一点,我包括了我为实现它和修改而执行的步骤,因此我可以将它保存到db以及文件系统中,我执行了以下步骤。如果您有任何疑问,请随时联系我,我将很乐意回答。

  1. 使用文章
  2. 中列出的代码在我的Web服务项目中创建了一个代码文件
  3. 创建了一些属性来存储值以保存到db

    private string _requestXml;
    private DateTime _start;
    
  4. 然后我修改了WriteInput方法,将值保存到那些变量中。

    public void WriteInput(SoapMessage message)
    {
        //Begin Edit
        oldStream.Position = 0;
        _requestXml = new StreamReader(_oldStream).ReadToEnd();
        _start = DateTime.UtcNow;
        //End Edit
        //Begin Original Code
        oldStream.Position = 0;
        Copy(oldStream, newStream);
        var fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
        var w = new StreamWriter(fs);
        var soapString = (message is SoapServerMessage) ? "SoapRequest" : "SoapResponse";
        w.WriteLine("-----" + soapString + " at " + DateTime.Now);
        w.Flush();
        newStream.Position = 0;
        Copy(newStream, fs);
        w.Close();
        newStream.Position = 0;
    }
    
  5. 然后我将WriteOutput修改为

    public void WriteOutput(SoapMessage message)
    {
        //Begin Edit
        var responseXml = new StreamReader(newStream).ReadToEnd();
        newStream.Position = 0;
        //Start process for saving to DB 
        //"_requestXml" = Original Request Soap Message
        //"responseXml" = Service Returned Response
        //"_start" = Request Start Time
        //message.MethodInfo.Name = I save this so I know what method from     
        //message.Url = I save this so I know the original ASMX that was hit
        //End Edit
        //Begin Original Code
        newStream.Position = 0;
        var fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
        var w = new StreamWriter(fs);
        var soapString = (message is SoapServerMessage) ? "SoapResponse" : "SoapRequest";
        w.WriteLine("-----" + soapString + " at " + DateTime.Now);
        w.Flush();
        Copy(newStream, fs);
        w.Close();
        newStream.Position = 0;
        Copy(newStream, oldStream);
    }
    
  6. 现在剩下的就是将以下内容添加到您的服务电话中,您应该好好去

    [WebMethod, NameSpace.OfyourTraceExtesionClass.TraceExtension]
    public void WebMethod1()
    {
        //DoSomeStuff
    }
    

答案 1 :(得分:0)

如果要记录http post消息,解决它的一种优雅方法是在Web服务前设置一个简单的反向代理。在此代理中,您可以完全访问原始http请求。

答案 2 :(得分:0)

  • 作为Stream
  • 收到的XML文件
  • 将Stream转换为byte []
  • 将byte []转换为XDocument(System.Xml.Linq)
  • 使用LINQ语法读取XDocument并保存到DB

答案 3 :(得分:0)

    [WebMethod]
    public void XMLPersing()
    {
        var XMLDATA = "";

        WriteLogCLS objWriteLog = new WriteLogCLS();
        Stream receiveStream = HttpContext.Current.Request.InputStream;
        receiveStream.Position = 0;
        StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);      //For xml persing..
        XMLDATA = readStream.ReadToEnd();
        readStream.Close();

      objWriteLog.WriteLog(Convert.ToString(XMLDATA));
        XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("Log/Exception/Sample.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlreader);
        xmlreader.Close();
        if (ds.Tables.Count != 0)
        {
            var strCon = string.Empty;
            strCon = ConfigurationManager.AppSettings["constring"];
            SqlCommand cmdInsertXMLData = new SqlCommand();
            SqlConnection SqlConn;
            SqlConn = new SqlConnection(strCon);
            try
            {
                cmdInsertXMLData = new SqlCommand("usp_InsertXML", SqlConn);
                cmdInsertXMLData.CommandType = CommandType.StoredProcedure;
                // cmdInsertLoginDetails.Parameters.Add("@XMLdata", SqlDbType.Xml).Value = ds.GetXml();
                cmdInsertXMLData.Parameters.AddWithValue("@XMLdata", SqlDbType.Xml);

                if (SqlConn.State == ConnectionState.Closed)
                {
                    SqlConn.Open();
                }
                cmdInsertXMLData.ExecuteNonQuery();
                // response = cmdInsertLoginDetails.Parameters["@Message"].Value.ToString();
            }
            catch (Exception ex)
            {
                objWriteLog.WriteLog("Error on XML Persing : " + ex.Message);
                // response = "Error";
            }
            finally
            {
                if (cmdInsertXMLData != null)
                {
                    cmdInsertXMLData.Dispose();
                }
                if (SqlConn.State == ConnectionState.Open)
                {
                    SqlConn.Close();
                    SqlConn.Dispose();
                }
                objWriteLog = null;
            }
            // return response  ;

        }
    }
}