Commit 52af3dca by martin.slowinski

some logic

parent 2aa07420
using System; using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// 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] [ServiceContract]
public interface IService public interface IService
{ {
...@@ -14,30 +7,8 @@ public interface IService ...@@ -14,30 +7,8 @@ public interface IService
[OperationContract] [OperationContract]
string GetData(int value); string GetData(int value);
[OperationContract] [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 ...@@ -14,16 +14,8 @@ public class Service : IService
return string.Format("You entered: {0}", value); return string.Format("You entered: {0}", value);
} }
public CompositeType GetDataUsingDataContract(CompositeType composite) public string GetPipeString()
{ {
if (composite == null) return "1|Martin|Poland|Lublin";
{ }
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
} }
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
using System.Xml; using System.Xml;
using System.Xml.Serialization; using System.Xml.Serialization;
using SoapConsumer.Infrastructure.Proxy.Builders.Abstract; using SoapConsumer.Infrastructure.Proxy.Builders.Abstract;
namespace SoapConsumer.Infrastructure.Proxy.Builders namespace SoapConsumer.Infrastructure.Proxy.Builders
{ {
public class SoapXmlBuilder : ISoapXmlBuilder public class SoapXmlBuilder : ISoapXmlBuilder
...@@ -10,29 +9,26 @@ namespace SoapConsumer.Infrastructure.Proxy.Builders ...@@ -10,29 +9,26 @@ namespace SoapConsumer.Infrastructure.Proxy.Builders
public string Build<T>(T value, string @namespace) public string Build<T>(T value, string @namespace)
{ {
var namespaces = new XmlSerializerNamespaces(); var namespaces = new XmlSerializerNamespaces();
namespaces.Add("", @namespace); namespaces.Add("tem", @namespace);
var serializer = new XmlSerializer(value.GetType(), @namespace); var serializer = new XmlSerializer(value.GetType(), @namespace);
var settings = new XmlWriterSettings var settings = new XmlWriterSettings
{ {
Indent = true, Indent = true,
OmitXmlDeclaration = true OmitXmlDeclaration = true
}; };
using (var stream = new StringWriter()) using (var stream = new StringWriter())
{ {
using (var writer = XmlWriter.Create(stream, settings)) using (var writer = XmlWriter.Create(stream, settings))
{ {
serializer.Serialize(writer, value, namespaces); serializer.Serialize(writer, value, namespaces);
} }
var xmlRoot = $@"<?xml version=""1.0"" encoding=""utf-8""?> var xmlRoot = $@"<soap12:Envelope
<soap12:Envelope xmlns:soap12=""http://schemas.xmlsoap.org/soap/envelope/""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:tem=""http://tempuri.org/"">
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
<soap12:Body> <soap12:Body>
{stream.ToString()} {stream.ToString()}
</soap12:Body > </soap12:Body>
</soap12:Envelope > "; </soap12:Envelope>";
return xmlRoot; return xmlRoot;
} }
} }
......
...@@ -36,10 +36,8 @@ namespace SoapConsumer.Infrastructure.Proxy ...@@ -36,10 +36,8 @@ namespace SoapConsumer.Infrastructure.Proxy
var host = _configuration.GetSection(soapAttribute.Host ?? "WebService:Hosts:0:DefaultHost") var host = _configuration.GetSection(soapAttribute.Host ?? "WebService:Hosts:0:DefaultHost")
.GetValue(); .GetValue();
//var endpoint = _configuration.GetSection(soapAttribute.Endpoint).GetValue(); var endpoint = _configuration.GetSection(soapAttribute.Endpoint).GetValue();
//var operation = _configuration.GetSection(soapAttribute.Operation).GetValue(); var operation = _configuration.GetSection(soapAttribute.Operation).GetValue();
var endpoint = soapAttribute.Endpoint;
var operation = soapAttribute.Operation;
var request = CreateWebRequest(host, endpoint, operation); var request = CreateWebRequest(host, endpoint, operation);
var soapEnvelopeXml = new XmlDocument(); var soapEnvelopeXml = new XmlDocument();
...@@ -68,8 +66,8 @@ namespace SoapConsumer.Infrastructure.Proxy ...@@ -68,8 +66,8 @@ namespace SoapConsumer.Infrastructure.Proxy
private HttpWebRequest CreateWebRequest(string host, string endpoint, string operation) private HttpWebRequest CreateWebRequest(string host, string endpoint, string operation)
{ {
var webRequest = (HttpWebRequest)WebRequest.Create($@"{host}{endpoint}/{operation}"); var webRequest = (HttpWebRequest)WebRequest.Create($@"{host}{endpoint}?op={operation}");
webRequest.Headers.Add(@"SOAP:Action"); webRequest.Headers.Add(@"SOAPAction", $"http://tempuri.org/IService/{operation}");
webRequest.ContentType = "text/xml;charset=\"utf-8\""; webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml"; webRequest.Accept = "text/xml";
webRequest.Method = "POST"; webRequest.Method = "POST";
......
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SoapConsumer.Infrastructure.CQRS.Queries; using SoapConsumer.Infrastructure.CQRS.Queries;
using SoapConsumer.Queries; using SoapConsumer.Queries.Data;
using SoapConsumer.Queries.PipeString;
namespace SoapConsumer.Controllers namespace SoapConsumer.Controllers
{ {
[Route("test")] [Route("Data")]
public class TestController : Controller public class DataController : Controller
{ {
private readonly IQueryBus _queryBus; private readonly IQueryBus _queryBus;
public TestController(IQueryBus queryBus) public DataController(IQueryBus queryBus)
{ {
_queryBus = queryBus; _queryBus = queryBus;
} }
[HttpGet("testaction")] [HttpGet("{id}")]
public async Task<IActionResult> TestAction() 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); 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 Autofac;
using SoapConsumer.Queries.Handlers; using SoapConsumer.Queries.Data;
using SoapConsumer.Queries.PipeString;
using SoapConsumer.Soap.Data;
using SoapConsumer.Soap.PipeString;
namespace SoapConsumer.Modules namespace SoapConsumer.Modules
{ {
...@@ -8,12 +11,21 @@ namespace SoapConsumer.Modules ...@@ -8,12 +11,21 @@ namespace SoapConsumer.Modules
protected override void Load(ContainerBuilder builder) protected override void Load(ContainerBuilder builder)
{ {
base.Load(builder); base.Load(builder);
RegisterQueryHandlers(builder); RegisterQueryHandlers(builder);
RegisterTransformers(builder);
} }
private void RegisterQueryHandlers(ContainerBuilder 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; 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; } public int Id { get; set; }
} }
......
using System.Threading.Tasks; using System.Threading.Tasks;
using SoapConsumer.Infrastructure.CQRS.Queries.Generic; using SoapConsumer.Infrastructure.CQRS.Queries.Generic;
using SoapConsumer.Infrastructure.Proxy.Abstract; 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; private readonly IWebServiceProxy _webServiceProxy;
public TestQueryHandler(IWebServiceProxy webServiceProxy) public GetDataQueryHandler(IWebServiceProxy webServiceProxy)
{ {
_webServiceProxy = 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; } 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 System;
using SoapConsumer.Infrastructure.Proxy.Attributes; using SoapConsumer.Infrastructure.Proxy.Attributes;
namespace SoapConsumer.Soap namespace SoapConsumer.Soap.Data
{ {
[Serializable] [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/")] Namespace = "http://tempuri.org/")]
public class GetData public class GetData
{ {
......
using System; namespace SoapConsumer.Soap.Data
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SoapConsumer.Soap
{ {
public class GetResult public class GetDataResult
{ {
public string Value { get; set; } 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 @@ ...@@ -20,6 +20,9 @@
"Operations": [ "Operations": [
{ {
"Operation": "GetData" "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