WebService调用过程

//SSL:

//1 AccountLogin.aspx.cs 获取页面content
前端引用#region 前端引用
CustomerLogonContent content = null;
content = CustomerFacade.GetCustomerLogonContent(logonRequest, CustomerEntityCreator.Current.GetCustomerSubscriptionInfo());

//2 CustomerFacade.cs
public static CustomerLogonContent GetCustomerLogonContent
    (CustomerLogonService logonService, CustomerSubscriptionInfo subscriptionInfo)
{
    return MessageProcessor.Initiate<CustomerLogonContent>("CustomerLogon_CustomerLogon", logonService, subscriptionInfo);
}
#endregion


//CommonLibrary MessageProcessor.cs
//三步:构造消息,处理消息(调用WebService),返回消息
构造消息对象,处理序列化的消息,返回结果#region 构造消息对象,处理序列化的消息,返回结果
public static T Initiate<T>(string msgName, params object[] bizContent) where T : class
{
    return Initiate<T>(0, msgName, bizContent);
}

public static T Initiate<T>(int timeout, string msgName, params object[] bizContent) where T : class
{
    // construct message
    RequestMessage requestMsg = ConstructRequestMessage(msgName, bizContent);

    // invoke webservice
    string response = InvokeDelegateWebMethod(SerializeMessage<RequestMessage>(requestMsg), timeout);

    if (string.IsNullOrEmpty(response))
    {
        throw new Exception("MPE WebService response a null or empty value.");
    }

    // deserialize response msg
    ResponseMessage responseMsg = DeserializeMessage<ResponseMessage>(response);

    return responseMsg.BizContent as T;
}
#endregion

构造消息对象,消息体有三部分Name,ServerInfo,ProcessStep,参数信息#region 构造消息对象,消息体有三部分Name,ServerInfo,ProcessStep,参数信息

        private static RequestMessage ConstructRequestMessage(string msgName, params object[] bizContent)
        {
            RequestMessage msg = new RequestMessage();
            msg.Name = msgName;
            msg.ServerInfo = new Newegg.MPE.Common.ServerInfo();
            msg.ServerInfo.JumpedDBAlias = MPERequestContext.JumpedDBAlias;
            msg.ServerInfo.QueryDBName = ConfigurationManager.ServerConfiguration.ServerInfo.QueryDBAlias;
            msg.ServerInfo.HisQueryDBAlias = ConfigurationManager.ServerConfiguration.ServerInfo.HisQueryDBAlias;
            msg.ServerInfo.ServerID = ConfigurationManager.ServerConfiguration.ServerInfo.ServerId;
            msg.ServerInfo.IsCARequest = string.Compare(StringResource.ThreadStorage_Value_RequestHost_CA,
                                    LogicalThreadContext.GetData(StringResource.ThreadStorage_Key_RequestHost) as string) == 0;
            msg.ServerInfo.IsUSRequest = string.Compare(StringResource.ThreadStorage_Value_RequestHost_Default,
                                    LogicalThreadContext.GetData(StringResource.ThreadStorage_Key_RequestHost) as string) == 0;
            msg.ServerInfo.CurrencyExchangeRate = (decimal)LogicalThreadContext.GetData(StringResource.ThreadStorage_Key_CurrencyExchangeRate);
            msg.ServerInfo.RequestHost = LogicalThreadContext.GetData(StringResource.ThreadStorage_Key_RequestHost) as string;
            msg.ServerInfo.IsMobileDevice = (bool)LogicalThreadContext.GetData(StringResource.ThreadStorage_Key_Is_Mobile_Device);
            msg.ProcessStep = MessageProcessStep.Init;
            if (bizContent != null)
            {
                foreach (object o in bizContent)
                {
                    msg.AddBizObject(o);
                }
            }
            return msg;
        }

#endregion

处理序列化的消息对象#region 处理序列化的消息对象
        private static string InvokeDelegateWebMethod(string requestMsg, int timeout)
        {
            return WebServiceCommand.Invoke<DelegateWebService>(timeout, DelegateWebService.Method_Process, requestMsg) as string;
        }


    // 在WebServiceCommand.cs中,期间省略了异常处理代码
    public static object Invoke<T>(int timeout, string methodName, params object[] parameters) where T : SoapHttpClientProtocol
        {
            WebServiceItem webServiceItem = WebServiceManager.GetWebServiceItem(typeof(T));
           
            MethodInfo methodInfo = webServiceItem.MethodHashTable[methodName];
            ServiceItem serviceItem = ConfigurationManager.WebServiceConfiguration.GetServiceItem(webServiceItem.Alias);
           
            HostInfo hostInfo = ConfigurationManager.ServerConfiguration.HostSettings.HostInfoCollection[serviceItem.GroupName];

            SoapHttpClientProtocol webServiceInstance = webServiceItem.CreatInstance();
           
            if (hostInfo.UseProxy)
            {
                webServiceInstance.Proxy = GetWebProxy(ConfigurationManager.ServerConfiguration.HostProxy);
            }
            if (timeout <= 0)
            {
                webServiceInstance.Timeout = serviceItem.Timeout;
            }
            else
            {
                webServiceInstance.Timeout = timeout;
            }

            if (hostInfo.InvokeOrderType == InvokeOrderType.Sequence)
            {
                foreach (string host in hostInfo.Hosts)
                {
                    webServiceInstance.Url = host + serviceItem.Path;
                    try
                    {
                        return methodInfo.Invoke(webServiceInstance, parameters);
                    }
                    catch (Exception e)
                    {
                        WebServiceLogger.WriteCallWebServiceFaildLog(webServiceItem.Alias, webServiceInstance.Timeout, webServiceInstance.Url, e);
                        invokeException = e;
                    }
                }
            }
            else
            {
                List<string> hosts = new List<string>(hostInfo.Hosts);
                if (hostInfo.InvokeOrderType == InvokeOrderType.RandomOthers)
                {
                    webServiceInstance.Url = hosts[0] + serviceItem.Path;
                    try
                    {
                        return methodInfo.Invoke(webServiceInstance, parameters);
                    }
                    catch (Exception e)
                    {
                        WebServiceLogger.WriteCallWebServiceFaildLog(webServiceItem.Alias, webServiceInstance.Timeout, webServiceInstance.Url, e);
                        invokeException = e;
                    }
                }
                Random hostRandom = new Random();
                int hostIndex;
                while (hosts.Count > 0)
                {
                    hostIndex = hostRandom.Next(hosts.Count);
                    webServiceInstance.Url = hosts[hostIndex] + serviceItem.Path;
                    try
                    {
                        return methodInfo.Invoke(webServiceInstance, parameters);
                    }
                    catch (Exception e)
                    {
                        WebServiceLogger.WriteCallWebServiceFaildLog(webServiceItem.Alias, webServiceInstance.Timeout, webServiceInstance.Url, e);
                        invokeException = e;
                    }
                    hosts.RemoveAt(hostIndex);
                }
            }
            //WebServiceLogger.ExecuteWebServiceException(webServiceItem.Alias, webServiceInstance.Timeout, webServiceInstance.Url);
            throw new Exception("invoke webservcie error,", invokeException);
        }
#endregion

转载自:http://www.cnblogs.com/jqcdq8/archive/2008/11/19/1336623.html

转载于:https://www.cnblogs.com/dannyqiu/articles/2010374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤来调用 SQL Server 存储过程: 1. 配置数据库连接:首先,你需要配置与 SQL Server 数据库的连接。你可以使用数据库连接字符串来指定数据库的服务器名称、身份验证信息和其他相关参数。 2. 创建 Web Service:接下来,你需要创建一个 Web Service,用于接收请求并调用存储过程。你可以使用任何支持 Web Service 的编程语言或框架,例如.NET、Java、Python等。 3. 调用存储过程:在 Web Service 的代码中,你可以使用相应的数据库连接库或驱动程序来连接到 SQL Server 数据库。然后,通过执行存储过程调用语句来调用存储过程。具体的调用方式可能因编程语言和库的不同而有所区别。 以下是一个示例 C# 代码片段,演示了如何使用 ADO.NET 调用 SQL Server 存储过程: ```csharp using System; using System.Data; using System.Data.SqlClient; public class MyWebService { public void CallStoredProcedure(string parameter1, string parameter2) { string connectionString = "Data Source=YourServer;Initial Catalog=YourDatabase;User ID=YourUsername;Password=YourPassword"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@Parameter1", SqlDbType.VarChar).Value = parameter1; command.Parameters.Add("@Parameter2", SqlDbType.VarChar).Value = parameter2; command.ExecuteNonQuery(); } } } } ``` 请注意,这只是一个示例,你需要根据自己的实际情况进行适当的修改和调整。 希望这可以帮助到你!如果你有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值