簡體   English   中英

將operationtimeout添加到代碼中實現的通道

[英]Add operationtimeout to channel implemented in code

我已經看到了如何轉換為代理的引用,例如:

((IContextChannel)client.InnerChannel).OperationTimeout = new TimeSpan(0,0,240); 

設置一個operationtimeout但我使用的是client = channelFactory.CreateChannel();

如何將頻道轉換為IContextChannel? 我希望這是有道理的。 我沒有完全掌握WCF中的頻道。

謝謝

阿列克謝

不確定如何實施您的建議。 在這段代碼中,我如何設置operationtimeout?

        try
        {
            Binding multipleTokensBinding = MultiAuthenticationFactorBinding.CreateMultiFactorAuthenticationBinding();
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            EndpointAddress endpointaddress = new EndpointAddress(new Uri("https://justsomeservice"), EndpointIdentity.CreateDnsIdentity("someone.com"));

            ChannelFactory<TransActionSvc.TransactionPortType> channelFactory = null;
            TransActionSvc.TransactionPortType client = null;

            channelFactory = new ChannelFactory<TransActionSvc.TransactionPortType>(multipleTokensBinding, endpointaddress);
            BindingElementCollection elements = channelFactory.Endpoint.Binding.CreateBindingElements();
            elements.Find<SecurityBindingElement>().IncludeTimestamp = true;

            channelFactory.Endpoint.Binding = new CustomBinding(elements);

            channelFactory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.Root, X509FindType.FindBySerialNumber, "xxx");
            channelFactory.Credentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.CurrentUser, StoreName.Root, X509FindType.FindBySerialNumber, "xxx");

            channelFactory.Credentials.UserName.UserName = Properties.Settings.Default.UserName;
            channelFactory.Credentials.UserName.Password = Properties.Settings.Default.Password;

            TransActionSvc.fetchTranDataAsAttachmentRequest req = new WF_Prod_Svc.TransActionSvc.fetchTranDataAsAttachmentRequest();
            TransActionSvc.fetchTranDataAsAttachmentResponse res = new WF_Prod_Svc.TransActionSvc.fetchTranDataAsAttachmentResponse();
            TransActionSvc.FetchTranDataAsAttachmentRq_Type reqtype = new WF_Prod_Svc.TransActionSvc.FetchTranDataAsAttachmentRq_Type();
            TransActionSvc.FetchTranDataAsAttachmentRs_Type restype = new WF_Prod_Svc.TransActionSvc.FetchTranDataAsAttachmentRs_Type();
            TransActionSvc.EndpointReferenceType endpntref = new WF_Prod_Svc.TransActionSvc.EndpointReferenceType();

            XmlAttribute actionAttrib1 = doc.CreateAttribute("soapenv", "mustUnderstand", "http://schemas.xmlsoap.org/soap/envelope/");
            actionAttrib1.Value = "0";
            XmlAttribute actionAttrib2 = doc.CreateAttribute("xmlns");
            actionAttrib2.Value = "http://schemas.xmlsoap.org/ws/2003/03/addressing";
            XmlAttribute[] objAcctionAtrb = new XmlAttribute[2];
            objAcctionAtrb.SetValue(actionAttrib1, 0);
            objAcctionAtrb.SetValue(actionAttrib2, 1);

            TransActionSvc.AttributedURI action = new WF_Prod_Svc.TransActionSvc.AttributedURI();
            action.AnyAttr = objAcctionAtrb;
            action.Value = "Transaction";

            TransActionSvc.AttributedURI messageid = new WF_Prod_Svc.TransActionSvc.AttributedURI();
            messageid.AnyAttr = objAcctionAtrb;
            messageid.Value = System.Guid.NewGuid().ToString();

            TransActionSvc.AttributedURI to = new WF_Prod_Svc.TransActionSvc.AttributedURI();
            to.AnyAttr = objAcctionAtrb;
            to.Value = "XGI";

            TransActionSvc.EndpointReferenceType endpointreference = new WF_Prod_Svc.TransActionSvc.EndpointReferenceType();
            TransActionSvc.ReferencePropertiesType referenceproperties = new WF_Prod_Svc.TransActionSvc.ReferencePropertiesType();

            if (Svc_Division.Parsed) { reqtype.division = Svc_Division.StringValue; }
            try{reqtype.startDate = Convert.ToDateTime(Svc_StartDate.StringValue);}
            catch (FormatException ex){LogMessageToFile("Invalid Start date. " + ex.Message);}
            try{reqtype.endDate = Convert.ToDateTime(Svc_EndDate.StringValue);}
            catch (FormatException ex){LogMessageToFile("Invalid End date. " + ex.Message);}
            if (Svc_DateType.StringValue == "T")
            {
                reqtype.transactionDateType = TransActionSvc.TransactionDateType_Enum.TransactionDate;
            }
            else
            {
                reqtype.transactionDateType = TransActionSvc.TransactionDateType_Enum.PostingDate;
            }
            switch (Svc_TransType.StringValue)
            {
                case "OOP":
                    reqtype.transactionType = TransActionSvc.TransactionType_Enum.OOP;
                    break;
                case "CHARGES":
                    reqtype.transactionType = TransActionSvc.TransactionType_Enum.CHARGES;
                    break;
                default:
                    reqtype.transactionType = TransActionSvc.TransactionType_Enum.ALL;
                    break;
            }

            System.Xml.XmlElement companyid = doc.CreateElement("companyId");
            companyid.InnerText = Properties.Settings.Default.CompanyID;
            System.Xml.XmlElement[] objectarray = new System.Xml.XmlElement[1];
            objectarray.SetValue(companyid, 0);
            referenceproperties.Any = objectarray;
            endpointreference.ReferenceProperties = referenceproperties;

            req.Action = action;
            req.MessageID = messageid;
            req.To = to;
            req.ReplyTo = endpointreference;
            req.fetchTranDataAsAttachment = reqtype;

            try
            {
                client = channelFactory.CreateChannel();

                //THIS DOES NOT WORK
                client.OperationTimeout = new TimeSpan(0,10,0); 

                res = client.fetchTranDataAsAttachment(req);
                if (res.fetchTranDataAsAttachmentResponse1.WFFaultList != null)
                {
                    LogLine = string.Format("FaultCode({0});FaultType({1});FaultReason({2});Severity({3})",
                                res.fetchTranDataAsAttachmentResponse1.WFFaultList[0].faultCode,
                                res.fetchTranDataAsAttachmentResponse1.WFFaultList[0].faultType,
                                res.fetchTranDataAsAttachmentResponse1.WFFaultList[0].faultReasonText,
                                res.fetchTranDataAsAttachmentResponse1.WFFaultList[0].severity);
                }
                if (res.fetchTranDataAsAttachmentResponse1.attachment != null)
                {
                    string attachFileName = res.fetchTranDataAsAttachmentResponse1.attachment.fileName;
                    byte[] filebytes = res.fetchTranDataAsAttachmentResponse1.attachment.binaryData.Value;
                    FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\" + res.fetchTranDataAsAttachmentResponse1.attachment.fileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
                    fs.Write(filebytes, 0, filebytes.Length);
                    fs.Close();
                    DateTime EndReceive = DateTime.Now;
                    TimeSpan elapsed = EndReceive.Subtract(BeginReceive);
                    LogLine = string.Format("Arguments: [{0}]; FileName: {1}; FileSize: {2} bytes; ElapsedTime: {3} seconds", arguments.Trim(), attachFileName, filebytes.Length.ToString(), elapsed.TotalSeconds.ToString());
                    LogMessageToFile(LogLine);
                }
            }
            catch (CommunicationException ex1)
            {
                Abort((IChannel)client, channelFactory);
                FaultException fe = null;
                Exception tmp = ex1;
                while (tmp != null)
                {
                    fe = tmp as FaultException;
                    if (fe != null)
                    {
                        break;
                    }
                    tmp = tmp.InnerException;
                }
                if (fe != null)
                {
                    string errmsg = string.Format("The server sent back a fault: {0}", fe.CreateMessageFault().Reason.GetMatchingTranslation().Text);
                    LogMessageToFile(errmsg);
                }
                else
                {
                    string errmsg = string.Format("The request failed with exception: {0}", ex1.Message.ToString());
                    LogMessageToFile(errmsg);
                }
            }
            catch (TimeoutException)
            {
                Abort((IChannel)client, channelFactory);
                string errmsg = string.Format("The request timed out ");
                DateTime EndReceive = DateTime.Now;
                TimeSpan elapsed = EndReceive.Subtract(BeginReceive);
                LogLine = string.Format("Arguments: [{0}]; Exception: {1}; ElapsedTime: {2} seconds", arguments.Trim(), errmsg, elapsed.TotalSeconds.ToString());
                LogMessageToFile(LogLine);
            }
            catch (Exception ex)
            {
                Abort((IChannel)client, channelFactory);
                string errmsg = string.Format("The request failed with unexpected exception: {0}", ex.Message.ToString());
                LogMessageToFile(errmsg);
            }
            finally
            {
                ((IChannel)client).Close();
                channelFactory.Close();
            }
        }

通過將通道工廠轉換為通道接口,您將不會獲得任何有用的(非空/非異常)結果,因為工廠沒有必要實現任何通道接口。

您可以將頻道轉換為其他頻道界面,如果您知道所使用的頻道類型(如您所參考的代碼),則可能會獲得有用的結果。

編輯:我認為((IContextChannel)channel).OperationTimeout = new TimeSpan(0,10,0); 應該管用。

注意:您的帖子標題與您的代碼不符...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM