0

I am trying to integrate A console app project into to a.netcore project. In my console app the project does exactly what I want it to do and build and run perfectly well but when copied into the .net core web application when I try to add the library references it is suggesting a completely different library from the ones I am using the console app. when I try to use the suggested libraries the project will not build or if I am using the exact same reference it will not work in the .net core web app.

private HttpWebResponse PutOnUri(string uri, string contentType, string body)
{
    try
    {
        byte[] bodyBytes = Encoding.UTF8.GetBytes(body);

        var client = (HttpWebRequest)HttpWebRequest.Create(uri);
        client.AllowAutoRedirect = false;
        client.AllowWriteStreamBuffering = false;

        client.Method = "PUT";
        client.ContentType = contentType;
        client.ContentLength = bodyBytes.Length;

        client.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
        return (HttpWebResponse)client.GetResponse();
    }
    catch (WebException e)
    {
        return (HttpWebResponse)e.Response;
    }
}


 private XmlDocument LoadXMLFromUri(string uri)
 {
     XmlDocument doc = new XmlDocument();
     doc.Load(uri);
     return doc;
 }

 private string ConvertXmlDocumentToString(XmlDocument doc)
 {
     StringWriter sw = new StringWriter();
     XmlTextWriter tx = new XmlTextWriter(sw);
     doc.WriteTo(tx);
     return sw.ToString();
 }

These are the reference I am using in the OLD Console app and it works fine:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

Additional functions (Uses XPAth):

private string GetValueFromDocumentByXPath(XmlDocument doc, string xpath)
{
    var nav = doc.CreateNavigator();
    var it = nav.Select(xpath, nameSpaceManager_);
    if (it.MoveNext())
    {
        return it.Current.Value;
    }

        return "";
    }

    private void SetValueToDocumentByXPath(XmlDocument doc, string xpath, string value)
    {
        var nav = doc.CreateNavigator();
        var it = nav.Select(xpath, nameSpaceManager_);
        if (it.MoveNext())
        {
            it.Current.SetValue(value);
        }
    }
Dev
  • 1,780
  • 3
  • 18
  • 46

1 Answers1

2

You will need to modify your code a bit.

    private XPathDocument LoadXMLFromUri(string uri)
    {
        var req = WebRequest.CreateHttp(uri);
        var resTask = req.GetResponseAsync();
        resTask.Wait();
        XPathDocument doc = new XPathDocument(resTask.Result.GetResponseStream());
        return doc;
    }

    private string ConvertXmlDocumentToString(XmlDocument doc)
    {
        StringWriter sw = new StringWriter();
        XmlWriter tx = XmlWriter.Create(sw);
        doc.WriteTo(tx);
        return sw.ToString();
    }

    private string GetValueFromDocumentByXPath(XPathDocument doc, string xpath)
    {
        var nav = doc.CreateNavigator();
        var it = nav.Select(xpath, nameSpaceManager_);
        if (it.MoveNext())
        {
            return it.Current.Value;
        }

        return "";
    }

    private void SetValueToDocumentByXPath(XPathDocument doc, string xpath, string value)
    {
        var nav = doc.CreateNavigator();
        var it = nav.Select(xpath, nameSpaceManager_);
        if (it.MoveNext())
        {
            it.Current.SetValue(value);
        }
    }

    private HttpWebResponse PutOnUri(string uri, string contentType, string body)
    {
        try
        {
            byte[] bodyBytes = Encoding.UTF8.GetBytes(body);

            var client = (HttpWebRequest)HttpWebRequest.Create(uri);
            client.Method = "PUT";
            client.ContentType = contentType;
            var reqStreamTask = client.GetRequestStreamAsync();
            reqStreamTask.Result.Write(bodyBytes, 0, bodyBytes.Length);
            reqStreamTask.Wait();
            var resTask = client.GetResponseAsync();
            resTask.Wait();
            return (HttpWebResponse) resTask.Result;
        }
        catch (WebException e)
        {
            return (HttpWebResponse)e.Response;
        }
    }
Vijay
  • 523
  • 4
  • 13
  • This resolves the error but I had to use System.Xml.Linq; but another error appears when using: doc.CreateNavigator() – Dev Feb 03 '17 at 14:50
  • In that case use XPathDocument. You will need to different Library. Your posted code did not indicate what you needed. – Vijay Feb 03 '17 at 15:04
  • I am not sure what I need because I do not understand the problem. I am using Xpath to query a document from the URL Edit Some elements then send Put back to the Url. – Dev Feb 03 '17 at 15:11
  • I am assuming In order for my existing app to work I would have to modify the majority of my code to something else maybe LINQ with XML because these libraries aren't supported? – Dev Feb 03 '17 at 15:13
  • I have added my other two functions that stop working – Dev Feb 03 '17 at 15:15
  • @Decoder94 Edited the answer to reflect your additional methods. – Vijay Feb 03 '17 at 15:29
  • Thanks!! That Compiled very well. I just need to address the HTTPWebResponse function now to be up and running There might be other ways of sending the request So I'll research how to add to your answer then mark as answered. Thanks @Vijay – Dev Feb 03 '17 at 15:35
  • @Decoder94 I have added that one too. I missed to copy it from my test soln. Small tweaks will be needed as I did not configure my server to accept PUT. – Vijay Feb 03 '17 at 16:05
  • Thanks @Vijay Works Well... Can the document returned from elementCollectionEntryDoc() be used in ConvertXmlDocumentToString as a parameter? – Dev Feb 03 '17 at 16:23
  • Thanks! It all Work now Fixed the parameter Issue. XPATH documents are not modifiable but that a separate issue – Dev Feb 03 '17 at 16:51