Commit 52af3dca by martin.slowinski

some logic

parent 2aa07420
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
......@@ -15,29 +8,7 @@ public interface IService
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
string GetPipeString();
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
......@@ -14,16 +14,8 @@ public class Service : IService
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
public string GetPipeString()
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
return "1|Martin|Poland|Lublin";
}
}
......@@ -2,7 +2,6 @@
using System.Xml;
using System.Xml.Serialization;
using SoapConsumer.Infrastructure.Proxy.Builders.Abstract;
namespace SoapConsumer.Infrastructure.Proxy.Builders
{
public class SoapXmlBuilder : ISoapXmlBuilder
......@@ -10,29 +9,26 @@ namespace SoapConsumer.Infrastructure.Proxy.Builders
public string Build<T>(T value, string @namespace)
{
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("", @namespace);
namespaces.Add("tem", @namespace);
var serializer = new XmlSerializer(value.GetType(), @namespace);
var settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true
};
using (var stream = new StringWriter())
{
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, value, namespaces);
}
var xmlRoot = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap12:Envelope
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
var xmlRoot = $@"<soap12:Envelope
xmlns:soap12=""http://schemas.xmlsoap.org/soap/envelope/""
xmlns:tem=""http://tempuri.org/"">
<soap12:Body>
{stream.ToString()}
</soap12:Body >
</soap12:Envelope > ";
</soap12:Body>
</soap12:Envelope>";
return xmlRoot;
}
}
......
......@@ -36,10 +36,8 @@ namespace SoapConsumer.Infrastructure.Proxy
var host = _configuration.GetSection(soapAttribute.Host ?? "WebService:Hosts:0:DefaultHost")
.GetValue();
//var endpoint = _configuration.GetSection(soapAttribute.Endpoint).GetValue();
//var operation = _configuration.GetSection(soapAttribute.Operation).GetValue();
var endpoint = soapAttribute.Endpoint;
var operation = soapAttribute.Operation;
var endpoint = _configuration.GetSection(soapAttribute.Endpoint).GetValue();
var operation = _configuration.GetSection(soapAttribute.Operation).GetValue();
var request = CreateWebRequest(host, endpoint, operation);
var soapEnvelopeXml = new XmlDocument();
......@@ -68,8 +66,8 @@ namespace SoapConsumer.Infrastructure.Proxy
private HttpWebRequest CreateWebRequest(string host, string endpoint, string operation)
{
var webRequest = (HttpWebRequest)WebRequest.Create($@"{host}{endpoint}/{operation}");
webRequest.Headers.Add(@"SOAP:Action");
var webRequest = (HttpWebRequest)WebRequest.Create($@"{host}{endpoint}?op={operation}");
webRequest.Headers.Add(@"SOAPAction", $"http://tempuri.org/IService/{operation}");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
......
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SoapConsumer.Infrastructure.CQRS.Queries;
using SoapConsumer.Queries;
using SoapConsumer.Queries.Data;
using SoapConsumer.Queries.PipeString;
namespace SoapConsumer.Controllers
{
[Route("test")]
public class TestController : Controller
[Route("Data")]
public class DataController : Controller
{
private readonly IQueryBus _queryBus;
public TestController(IQueryBus queryBus)
public DataController(IQueryBus queryBus)
{
_queryBus = queryBus;
}
[HttpGet("testaction")]
public async Task<IActionResult> TestAction()
[HttpGet("{id}")]
public async Task<IActionResult> GetData(int id)
{
var result = await _queryBus.ExecuteAsync<TestQuery, TestQueryResponse>(new TestQuery { Id = 1 });
var result = await _queryBus.ExecuteAsync<GetDataQuery, GetDataQueryResult>(new GetDataQuery {Id = id});
return Ok(result.Data);
}
}
[HttpGet("PipeString")]
public async Task<IActionResult> PipeString()
{
var result = await _queryBus.ExecuteAsync<GetPipeStringQuery, GetPipeStringQueryResult>(new GetPipeStringQuery());
return Ok(result);
}
}
}
using Autofac;
using SoapConsumer.Queries.Handlers;
using SoapConsumer.Queries.Data;
using SoapConsumer.Queries.PipeString;
using SoapConsumer.Soap.Data;
using SoapConsumer.Soap.PipeString;
namespace SoapConsumer.Modules
{
......@@ -8,12 +11,21 @@ namespace SoapConsumer.Modules
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
RegisterQueryHandlers(builder);
RegisterTransformers(builder);
}
private void RegisterQueryHandlers(ContainerBuilder builder)
{
builder.RegisterType<TestQueryHandler>().AsImplementedInterfaces();
builder.RegisterType<GetDataQueryHandler>().AsImplementedInterfaces();
builder.RegisterType<GetPipeStringQueryHandler>().AsImplementedInterfaces();
}
private void RegisterTransformers(ContainerBuilder builder)
{
builder.RegisterType<GetDataTransformer>().AsImplementedInterfaces();
builder.RegisterType<GetPipeStringTransformer>().AsImplementedInterfaces();
}
}
}
using SoapConsumer.Infrastructure.CQRS.Queries.Generic;
namespace SoapConsumer.Queries
namespace SoapConsumer.Queries.Data
{
public class TestQuery : IQuery<TestQueryResponse>
public class GetDataQuery : IQuery<GetDataQueryResult>
{
public int Id { get; set; }
}
......
using System.Threading.Tasks;
using SoapConsumer.Infrastructure.CQRS.Queries.Generic;
using SoapConsumer.Infrastructure.Proxy.Abstract;
using SoapConsumer.Soap;
using SoapConsumer.Soap.Data;
namespace SoapConsumer.Queries.Handlers
namespace SoapConsumer.Queries.Data
{
public class TestQueryHandler : IHandleQuery<TestQuery, TestQueryResponse>
public class GetDataQueryHandler : IHandleQuery<GetDataQuery, GetDataQueryResult>
{
private readonly IWebServiceProxy _webServiceProxy;
public TestQueryHandler(IWebServiceProxy webServiceProxy)
public GetDataQueryHandler(IWebServiceProxy webServiceProxy)
{
_webServiceProxy = webServiceProxy;
}
public async Task<TestQueryResponse> HandleAsync(TestQuery query)
public async Task<GetDataQueryResult> HandleAsync(GetDataQuery query)
{
var a = await _webServiceProxy.ExecuteAsync<GetResult, GetData>(new GetData { value = 1 });
var result = await _webServiceProxy.ExecuteAsync<GetDataResult, GetData>(new GetData { value = query.Id });
return new TestQueryResponse
return new GetDataQueryResult
{
Data = "done from response"
Data = result.Value
};
}
}
......
namespace SoapConsumer.Queries
namespace SoapConsumer.Queries.Data
{
public class TestQueryResponse
public class GetDataQueryResult
{
public string Data { get; set; }
}
......
using SoapConsumer.Infrastructure.CQRS.Queries.Generic;
namespace SoapConsumer.Queries.PipeString
{
public class GetPipeStringQuery : IQuery<GetPipeStringQueryResult>
{
}
}
using System.Threading.Tasks;
using SoapConsumer.Infrastructure.CQRS.Queries.Generic;
using SoapConsumer.Infrastructure.Proxy.Abstract;
using SoapConsumer.Soap.PipeString;
namespace SoapConsumer.Queries.PipeString
{
public class GetPipeStringQueryHandler : IHandleQuery<GetPipeStringQuery, GetPipeStringQueryResult>
{
private readonly IWebServiceProxy _webServiceProxy;
public GetPipeStringQueryHandler(IWebServiceProxy webServiceProxy)
{
_webServiceProxy = webServiceProxy;
}
public async Task<GetPipeStringQueryResult> HandleAsync(GetPipeStringQuery query)
{
var result = await _webServiceProxy.ExecuteAsync<GetPipeStringResult, GetPipeString>(new GetPipeString());
return new GetPipeStringQueryResult
{
Id = result.Id,
Country = result.Country,
Name = result.Name,
Town = result.Town
};
}
}
}
namespace SoapConsumer.Queries.PipeString
{
public class GetPipeStringQueryResult
{
public string Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Town { get; set; }
}
}
\ No newline at end of file
using System;
using SoapConsumer.Infrastructure.Proxy.Attributes;
namespace SoapConsumer.Soap
namespace SoapConsumer.Soap.Data
{
[Serializable]
[Soap(Endpoint = "Service.svc/IService", Operation = "GetData",
[Soap(Endpoint = "WebService:Endpoints:0:Endpoint", Operation = "WebService:Endpoints:0:Operations:0:Operation",
Namespace = "http://tempuri.org/")]
public class GetData
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SoapConsumer.Soap
namespace SoapConsumer.Soap.Data
{
public class GetResult
public class GetDataResult
{
public string Value { get; set; }
}
......
using System;
using System.Xml;
using SoapConsumer.Infrastructure.Transformers.Abstract;
namespace SoapConsumer.Soap.Data
{
public class GetDataTransformer : ITransformer<GetDataResult>
{
public GetDataResult Transform(string responseData)
{
if (string.IsNullOrEmpty(responseData))
{
throw new Exception(
$"Received contractor creation response result is null. Parameter name: {nameof(responseData)}");
}
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(responseData);
return new GetDataResult
{
Value = xmlDocument.GetElementsByTagName("GetDataResult")[0].InnerText
};
}
}
}
using System;
using SoapConsumer.Infrastructure.Proxy.Attributes;
namespace SoapConsumer.Soap.PipeString
{
[Serializable]
[Soap(Endpoint = "WebService:Endpoints:0:Endpoint", Operation = "WebService:Endpoints:0:Operations:1:Operation",
Namespace = "http://tempuri.org/")]
public class GetPipeString
{
}
}
namespace SoapConsumer.Soap.PipeString
{
public class GetPipeStringResult
{
public string Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Town { get; set; }
}
}
using System;
using System.Xml;
using SoapConsumer.Infrastructure.Transformers.Abstract;
namespace SoapConsumer.Soap.PipeString
{
public class GetPipeStringTransformer : ITransformer<GetPipeStringResult>
{
public GetPipeStringResult Transform(string responseData)
{
if (string.IsNullOrEmpty(responseData))
{
throw new Exception(
$"Received contractor creation response result is null. Parameter name: {nameof(responseData)}");
}
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(responseData);
//dummy serialization :)
var arr = xmlDocument.GetElementsByTagName("GetPipeStringResult")[0].InnerText.Split('|');
return new GetPipeStringResult()
{
Id = arr[0],
Name = arr[1],
Country = arr[2],
Town = arr[3]
};
}
}
}
......@@ -20,6 +20,9 @@
"Operations": [
{
"Operation": "GetData"
},
{
"Operation": "GetPipeString"
}
]
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment