如何使用WEB服务?

时间:2014-10-07 09:37:08

标签: c# asp.net wcf wcf-binding

我创建了一个用于保存错误日志的Web服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Configuration;
using System.Data.Common;
using System.Data;
using System.Net.Mail;

using System.IO;

namespace TestErrorHandling
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public int SaveErrorLog(CompositeType objCom)
        {
            int messageId = 0;

            try
            {
                SqlDatabase _errDBConnection = null;
                _errDBConnection = new SqlDatabase(ConfigurationManager.ConnectionStrings["ErrorLogConnStr"].ToString());

                DbCommand dbCommand = _errDBConnection.GetStoredProcCommand("usp_SaveErrorLog");
                _errDBConnection.AddInParameter(dbCommand, "@i_ApplicationId", DbType.Int32, objCom.AppId);
                _errDBConnection.AddInParameter(dbCommand, "@i_ExceptionType", DbType.String, objCom.ExceptionType);
                _errDBConnection.AddOutParameter(dbCommand, "@O_MESSAGEID", DbType.Int32, 4);
                _errDBConnection.ExecuteReader(dbCommand);
                messageId = Convert.ToInt32(_errDBConnection.GetParameterValue(dbCommand, "@O_MESSAGEID"));
            }
            catch (Exception ex)
            {
                   throw new FaultException(ex.Message);
            }
            return messageId;
        }


    }
}

现在我在我的网络应用程序中调用此服务

using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Specialized;
using Test.ServiceReference1;

namespace Test
{
    public partial class _Default : BasePage
    {
            ServiceReference1.Service1Client obj1 = new  ServiceReference1.Service1Client;

                obj1.

}

但输入obj1后。它没有显示Service的SaveErrorLog方法。 请帮我解决这个问题。

添加了enter image description here

1 个答案:

答案 0 :(得分:1)

更改

ServiceReference1.Service1Client obj1 = new  ServiceReference1.Service1Client;

ServiceReference1.Service1Client obj1 = new  ServiceReference1.Service1Client();

然后使用

obj1.<method name>

还使用添加服务引用添加引用

像这样...... enter image description here

enter image description here

enter image description here

enter image description here