SSIS脚本任务Web服务错误

时间:2014-07-31 14:41:37

标签: c# web-services wcf ssis

我在SSIS包中有一个脚本任务来调用Web服务(WCF)。我所要做的就是向Web服务发送请求,我将收到一个结果(1或0)。但是如果失败并出现以下错误消息。当我将代码放在窗口应用程序窗体中时,它可以工作。

我几乎从窗口应用程序中复制了代码并尝试使其在SSIS中工作。不幸的是,我不知道Web服务是如何工作的,也不知道从哪里开始寻找解决方案。我需要做任何绑定吗?有一个应用程序配置文件,但它说CustomBinding。我再次为我缺乏知识而道歉,我完全无能为力。任何帮助,将不胜感激。谢谢!

  

at System.RuntimeMethodHandle.InvokeMethod(Object target,Object [] arguments,Signature sig,Boolean constructor)      at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object [] parameters,Object [] arguments)      在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)      at System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams)      在Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

以下是我在脚本任务中的代码。

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();
        }
    }

}

2 个答案:

答案 0 :(得分:1)

您可以将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源读取此XML并将其放置到记录集目标或表中。

答案 1 :(得分:0)

事实证明SSIS无法访问脚本任务中的app配置文件。这就是我必须在代码中编写绑定的原因。我刚刚添加了以下绑定代码,但它确实有效。

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);