簡體   English   中英

SSIS腳本任務Web服務錯誤

[英]SSIS Script Task Web Service error

我在SSIS包中有一個腳本任務來調用Web服務(WCF)。 我要做的就是將請求發送到Web服務,然后我將收到結果(1或0)。 但是,如果失敗並顯示以下錯誤消息。 當我將代碼放在窗口應用程序窗體中時,它可以工作。

我幾乎從窗口應用程序中復制了代碼,並試圖使其在SSIS中工作。 不幸的是,我不知道Web服務如何工作,也不知道從哪里開始尋找解決方案。 我需要綁定嗎? 有一個應用程序配置文件,但顯示CustomBinding。 再次,我為自己的知識不足而道歉,我一無所知。 任何幫助,將不勝感激。 謝謝!

在System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object []參數,Object []參數)在System.Reflection.RuntimeMethodInfo處System.RuntimeMethodHandle.InvokeMethod(對象目標,Object []參數,簽名sig,布爾構造函數)。在System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder活頁夾,對象目標,Object []提供了Args,ParameterModifier []修飾符,CultureInfo文化,位於Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()的String []命名為Params)

以下是我在腳本任務中的代碼。

public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{

    public void Main()
    {
        // TODO: Add your code here                

        ServiceReference.SendMessageClient svc = new ServiceReference.SendMessageClient();


        MessageCredentialsHeader MessageSecurityHeader = new MessageCredentialsHeader("user", "pw");
        try
        {
            using (System.ServiceModel.OperationContextScope contextScope = new System.ServiceModel.OperationContextScope(svc.InnerChannel))
            {
                System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(MessageSecurityHeader);

                MessageBox.Show(svc.SendMessage("111", "222", "SSIS test").Result.ToString());

            }
        }
        catch (Exception ex)
        {
            string s = ex.Message;
        }

        Dts.TaskResult = (int)ScriptResults.Success;
    }


    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
    #endregion
    public class MessageCredentialsHeader: System.ServiceModel.Channels.MessageHeader
    {

        public string Username { get; set; }
        public string Password { get; set; }

        public MessageCredentialsHeader(string username, string password)
        {
            Username = username;
            Password = password;
        }

        public override string Name
        {
            get { return "Security"; }
        }

        public override string Namespace
        {
            get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
        }

        public override bool MustUnderstand
        {
            get
            {
                return true;
            }
        }

        protected override void OnWriteStartHeader(XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion messageVersion)
        {
            base.OnWriteStartHeader(writer, messageVersion);
            string prefix = writer.LookupPrefix("http://schemas.xmlsoap.org/soap/envelope/");
            writer.WriteAttributeString(prefix + ":actor", "http://example.org/ws/webservicesecurity");
        }

        protected override void OnWriteHeaderContents(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion messageVersion)
        {
            writer.WriteStartElement("UsernameToken", Namespace);
            writer.WriteStartElement("Username", Namespace);
            writer.WriteRaw(Username);
            writer.WriteEndElement();
            writer.WriteStartElement("Password", Namespace);
            writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
            writer.WriteRaw(Password);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }

}

您可以將readwrite參數傳遞給腳本任務-例如securityResponse。 您首先需要在包中創建一個變量,並帶有xml響應的值。 然后,在腳本代碼中,您可以添加諸如此類的代碼,以使您朝着正確的方向發展:

 object nativeObject = Dts.Connections["Webservice"].AcquireConnection(null);
 HttpClientConnection conn = new HttpClientConnection(nativeObject);

 Service ws = new Service(conn.ServerURL);
 securityRequest req = new securityRequest();
 req.username = "****";
 req.password = "****";

 securityResponse response = ws.GetSecurityResp(req);

 System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(response.GetType());
 StringWriterWithEncoding responseToXml = new StringWriterWithEncoding(new StringBuilder(), Encoding.UTF8);

 x.Serialize(responseToXml, response);
 Dts.Variables["User::securityResponse"].Value = responseToXml.ToString();
 Dts.TaskResult = (int)ScriptResults.Success;

然后,您將需要使用XML源創建另一個數據流任務,該任務將讀取XML並將其放置到記錄集目標或表中。

事實證明,SSIS無法在腳本任務中訪問應用程序配置文件。 這就是為什么我必須用代碼編寫綁定的原因。 我剛剛添加了以下綁定代碼,它可以正常工作。

HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();

        httpsTransport.ManualAddressing = false;
        httpsTransport.MaxBufferPoolSize = 524288;
        httpsTransport.MaxReceivedMessageSize = 65536;
        httpsTransport.AllowCookies = false;
        httpsTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
        httpsTransport.BypassProxyOnLocal = false;
        httpsTransport.DecompressionEnabled = true;
        httpsTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        httpsTransport.KeepAliveEnabled = true;
        httpsTransport.MaxBufferSize = 65536;
        httpsTransport.ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
        httpsTransport.Realm = "";
        httpsTransport.TransferMode = TransferMode.Buffered;
        httpsTransport.UnsafeConnectionNtlmAuthentication = false;
        httpsTransport.UseDefaultWebProxy = true;
        httpsTransport.RequireClientCertificate = false;

        TextMessageEncodingBindingElement encoding = new TextMessageEncodingBindingElement();
        encoding.MessageVersion = MessageVersion.Soap11;
        encoding.WriteEncoding = Encoding.UTF8;
        encoding.MaxReadPoolSize = 64;
        encoding.MaxWritePoolSize = 16;
        encoding.ReaderQuotas.MaxDepth = 32;
        encoding.ReaderQuotas.MaxStringContentLength = 8192;
        encoding.ReaderQuotas.MaxArrayLength = 16384;
        encoding.ReaderQuotas.MaxBytesPerRead = 4096;
        encoding.ReaderQuotas.MaxNameTableCharCount = 16384;

        CustomBinding binding = new CustomBinding();
        binding.Name = "SendMessage";
        binding.Elements.Add(encoding);
        binding.Elements.Add(httpsTransport);

        EndpointAddress endPoint = new EndpointAddress("https://example.org/SendMessage");

        ServiceReference.SendMessageClient svc = new ServiceReference.SendMessageClient (binding, endPoint);

暫無
暫無

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

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