Change c# WebReference url address - c#

Hi,
I have 2 clients with 2 different servers.
After generating wsdl classes I change url address for clients accordingly in SoapHttpClientProtocol consructor.
from
this.Url = "http://10.0.3.5:88/SomeName/dish
to
this.Url = "http://192.168.20.5:88/SomeOtherName/dish
But I can't change SoapDocumentMethodAttribute at runtime. Without changing it my method doesn't return DataSet just null. After changing all addresses in attribute everything works fine.
[System.Web.Services.Protocols.SoapDocumentMethodAttribute( "http://10.0.3.5:88/SomeName/EuroSoft/ProductTransferExecute", RequestNamespace = "http://10.0.3.5:88/SomeName/dish", ResponseNamespace = "http://10.0.3.5:88/SomeName/dish", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle =
System.Web.Services.Protocols.SoapParameterStyle.Wrapped )]
public System.Data.DataSet ProductTransferExecute( [System.Xml.Serialization.XmlElementAttribute( IsNullable = true )] string department, [System.Xml.Serialization.XmlElementAttribute( IsNullable = true )] string XMLproducts, out int sqlcode ) {}
Services are generated by Sybase Anywhere 9 database. Is it possible to change it dynamic? What needs to be identical for this to work?

Create a CustomSoapHttpClientProtocol:
public class CustomSoapHttpClientProtocol : SoapHttpClientProtocol
{
public string SoapActionUrl { get; private set; }
public CustomSoapHttpClientProtocol(string soapActionUrl)
{
this.SoapActionUrl = soapActionUrl;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
const string soapAction = "SOAPAction";
if (request.Headers.Count > 0 && request.Headers.AllKeys.Contains(soapAction))
{
request.Headers[soapAction] = SoapActionUrl;
}
WebResponse response = base.GetWebResponse(request);
return response;
}
Then in your proxy class replace SoapHttpClientProtocol with your CustomSoapHttpClientProtocol.

Related

How can I return XML from BeforeSendRequest and AfterReceiveReply to the calling method in a thread-safe way?

We have a console application using the Azure WebJob SDK. The WebJob relies on a WCF service using SOAP, which it accesses through a DLL we wrote that wraps the auto-generated WCF types in something a bit more friendly.
For logging purposes, we want to save the request and response XML bodies for requests that we make. These XML bodies would be saved in our database. But, because the WCF code lives in a low-level DLL, it has no concept of our database and can't save to it.
The DLL uses Microsoft's DI extensions to register types, and the WebJob calls into it like this:
class WebJobClass
{
IWCFWrapperClient _wcfWrapperClient;
public WebJobClass(IWCFWrapperClient wcfWrapperClient)
{
_wcfWrapperClient = wcfWrapperClient;
}
public async Task DoThing()
{
var callResult = await _wcfWrapperClient.CallWCFService();
}
}
IWCFWrapperClient looks like this:
class WCFWrapperClient : IWCFWrapperClient
{
IWCF _wcf; // auto-generated by VS, stored in Reference.cs
public async Task<object> CallWCFService()
{
return await _wcf.Call(); // another auto-generated method
}
}
I've implemented an IClientMessageInspector, and it works fine to get me the XML request/response, but I don't have a way to pass it back up to WCFWrapperClient.CallWCFService so that it can be returned to WebJobClass.DoThing(), who could then save it to the database.
The problem is multithreading. WebJobs, IIRC, will run multiple requests in parallel, calling into the DLL from multiple threads. This means we can't, say, share a static property LastRequestXmlBody since multiple threads could overwrite it. We also can't, say, give each call a Guid or something since there's no way to pass anything from IWCFWrapperClient.CallWCFService into the auto-generated IWCF.Call except what was auto-generated.
So, how can I return XML to WebJobClass.DoThing in a thread-safe way?
I was able to find a solution that uses ConcurrentDictionary<TKey, TValue>, but it's a bit ugly.
First, I amended the auto-generated classes in Reference.cs with a new property Guid InternalCorrelationId. Since the auto-generated classes are partial, this can be done in separate files that aren't changed when the client is regenerated.
public partial class AutoGeneratedWCFType
{
private Guid InternalCorrelationIdField;
[System.Runtime.Serialization.DataMember()]
public Guid InternalCorrelationId
{
get { return InternalCorrelationIdField; }
set { InternalCorrelationIdField = value; }
}
}
Next, I made all my request DTO types derive from a type named RequestBase, and all my response DTO types derive from a typed named ResponseBase, so I could handle them generically:
public abstract class RequestBase
{
public Guid InternalCorrelationId { get; set; }
}
public abstract class ResponseBase
{
public string RequestXml { get; set; }
public string ResponseXml { get; set; }
}
I then added a type RequestCorrelator that simply holds on to a ConcurrentDictionary<Guid, XmlRequestResponse>:
public sealed class RequestCorrelator : IRequestCorrelator
{
public ConcurrentDictionary<Guid, XmlRequestResponse> PendingCalls { get; }
public RequestCorrelator() => PendingCalls = new ConcurrentDictionary<Guid, XmlRequestResponse>();
}
public sealed class XmlRequestResponse
{
public string RequestXml { get; set; }
public string ResponseXml { get; set; }
}
RequestCorrelator is its own type for DI purposes - you may just be able to use a ConcurrentDictionary<TKey, TValue> directly.
Finally, we have the code that actually grabs the XML, a type implementing IClientMessageInspector:
public sealed class ClientMessageProvider : IClientMessageInspector
{
private readonly IRequestCorrelator _requestCorrelator;
public ClientMessageProvider(IRequestCorrelator requestCorrelator) =>
_requestCorrelator = requestCorrelator;
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
var requestXml = request.ToString();
var internalCorrelationId = GetInternalCorrelationId(requestXml);
if (internalCorrelationId != null)
{
if (_requestCorrelator.PendingCalls.TryGetValue(internalCorrelationId.Value,
out var requestResponse))
{
requestResponse.RequestXml = requestXml;
}
request = RemoveInternalCorrelationId(request);
}
return internalCorrelationId;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
// WCF can internally correlate a request between BeforeSendRequest and
// AfterReceiveReply. We reuse the same correlation ID we added to the
// XML as our correlation state.
var responseXml = reply.ToString();
var internalCorrelationId = (correlationState is Guid guid)
? guid
: default;
if (_requestCorrelator.PendingCalls.TryGetValue(internalCorrelationId,
out var requestResponse))
{
requestResponse.ResponseXml = responseXml;
}
}
private static Guid? GetInternalCorrelationId(string requestXml)
{
var document = XDocument.Parse(requestXml);
var internalCorrelationIdElement = /* You'll have to write this yourself;
every WCF XML request is different. */
return internalCorrelationIdElement != null
? Guid.Parse(internalCorrelationIdElement.Value)
: null;
}
private static Message RemoveInternalCorrelationId(Message oldMessage)
{
// https://stackoverflow.com/a/35639900/2709212
var buffer = oldMessage.CreateBufferedCopy(2 * 1024 * 1024);
var tempMessage = buffer.CreateMessage();
var dictionaryReader = tempMessage.GetReaderAtBodyContents();
var document = new XmlDocument();
document.Load(dictionaryReader);
dictionaryReader.Close();
var internalCorrelationIdNode = /* You'll also have to write this yourself. */
var parent = internalCorrelationIdNode.ParentNode;
parent.RemoveChild(internalCorrelationIdNode);
var memoryStream = new MemoryStream();
var xmlWriter = XmlWriter.Create(memoryStream);
document.Save(xmlWriter);
xmlWriter.Flush();
xmlWriter.Close();
memoryStream.Position = 0;
var xmlReader = XmlReader.Create(memoryStream);
var newMessage = Message.CreateMessage(oldMessage.Version, null, xmlReader);
newMessage.Headers.CopyHeadersFrom(oldMessage);
newMessage.Properties.CopyProperties(oldMessage.Properties);
return newMessage;
}
}
In short, this type:
Finds the correlation ID in the XML request.
Finds the XmlRequestResponse with the same correlation ID and adds the request to it.
Removes the correlation ID element so that the service doesn't get elements they didn't expect.
After receiving a reply, uses correlationState to find the XmlRequestResponse and write the response XML to it.
Now all we have to do is change IWCFWrapperClient:
private async Task<TDtoResult> ExecuteCallWithLogging<TDtoRequest,
TWcfRequest,
TWcfResponse,
TDtoResult>(TDtoRequest request,
Func<TDtoRequest, TWcfRequest> dtoToWcfConverter,
Func<TWcfRequest, Task<TWcfResponse>> wcfCall,
Func<TWcfResponse, TDtoResult> wcfToDtoConverter)
where TDtoRequest : CorrelationBase
where TDtoResult : WcfBase
{
request.InternalCorrelationId = Guid.NewGuid();
var xmlRequestResponse = new XmlRequestResponse();
_requestCorrelator.PendingCalls.GetOrAdd(request.InternalCorrelationId,
xmlRequestResponse);
var response = await contractingCall(dtoToWcfConverter(request));
_requestCorrelator.PendingCalls.TryRemove(request.InternalCorrelationId, out _);
return wcfToDtoConverter(response).WithRequestResponse(xmlRequestResponse);
}
public async Task<DoThingResponseDto> DoThing(DoThingRequestDto request) =>
await ExecuteCallWithLogging(request,
r => r.ToWcfModel(),
async d => await _wcf.Call(d),
d => d.ToDtoModel());
WithRequestResponse is implemented as follows:
public static T WithRequestResponse<T>(this T item, XmlRequestResponse requestResponse)
where T : ResponseBase
{
item.RequestXml = requestResponse?.RequestXml;
item.ResponseXml = requestResponse?.ResponseXml;
return item;
}
And there we go. WCF calls that return their XML in the response object rather than just something you can print to console or log to a file.

How to use datacontract in C#?

I have this interface both in server and client side:
namespace BH_Server {
[ServiceContract]
public interface BHInterface {
[OperationContract]
string GetName( string name );
[OperationContract]
Device GetDevice();
}
[DataContract]
public class Device {
private string dSN;
[DataMember]
public string SN {
get { return dSN; }
set { dSN = value; }
}
}
}
Also, I have this in server side:
public class CronServiceInterface : BHInterface {
public string GetName( string name ) {
return string.Format( "Hello {0}", name );
}
public Device GetDevice() {
Device d = new Device();
d.SN = "123456789";
return d;
}
}
And this on server side, also:
host = new ServiceHost( typeof( CronServiceInterface ), new Uri[] {
new Uri("net.pipe://localhost/")
} );
host.AddServiceEndpoint( typeof( BHInterface ), new NetNamedPipeBinding( NetNamedPipeSecurityMode.None ), "BhPipe" );
host.Open();
To create connection on client side, this code is used:
NetNamedPipeBinding binding = new NetNamedPipeBinding( NetNamedPipeSecurityMode.None );
ChannelFactory<BHInterface> channelFactory = new ChannelFactory<BHInterface>( binding );
EndpointAddress endpointAddress = new EndpointAddress( "net.pipe://localhost/BhPipe/" );
BHInterface iface = channelFactory.CreateChannel( endpointAddress );
Obviously not all the code is written here, I hope it is enough to see what is implemented.
Using Debug.WriteLine( iface.GetName("Tom") ); results "Hello Tom" in client side, but the following code won't work:
Device d;
d = iface.GetDevice();
Debug.WriteLine( string.Format( "Printing sn: {0}", d.SN ) );
It prints: "Printing sn: ".
I'm using .NET 4.5 and error is not thrown. I'm new in WCF topic.
Would somebody so kind explaining to me how could I pass the desired object to client?
To elaborate a bit more... Like the ServiceContract and OperationContract shows the prototypes veses implementation, so goes for the DataContract and DataMembers. You are placing implementation
get { return dSN; }
set { dSN = value; }
Where all is needed is the
public string SN {get;set;}
To solve this just remove the backing field for your property and have DataContract defined as
[DataContract]
public class Device {
[DataMember]
public string SN {get;set;}
}
The reason is that the value of dSN is not sent from the service to the client because it is not a [DataMember]. Other solution would be mark the private field with [DataMember] attribute but you should generally avoid such practice.
Also , remember to update service reference after any change to the data contracts as otherwise client will still see old contracts.
Huh, I found out!
I had to use attributes in my datacontracts!
namespace BH_Server {
[ServiceContract]
public interface BHInterface {
[OperationContract]
string GetName( string name );
[OperationContract]
Device GetDevice();
}
[DataContract( Name = "Device", Namespace = "" )]
public class Device {
[DataMember( Name = "SN", Order = 1 )]
public string SN { get; set; }
}
}
Now it works like a charm!

wcf rest json return collection

I am attempting to build a Restful WCF Service which returns data in JSON format. My firsts methods work fine but when I try return a collection my test program receive the next exception:
Unable to write data to the transport connection. An existing connection was forcibly closed by the remote host.
My Service code:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetModes")]
OGetModesResponse OGetModes(OGetModesRequest oGetModes);
}
[DataContract]
public class OGetModesRequest
{
private String m_sTicket;
[DataMember]
public String prTicket
{
get { return m_sTicket; }
set { m_sTicket = value; }
}
}
[DataContract]
public class OGetModesResponse
{
[DataMember]
public string sTicket;
[DataMember]
public emStatus emStatus;
[DataMember]
public IList<CTMode> aoModes;
}
And my test program:
OGetModesRequest oGetModes = new OGetModesRequest { prTicket = sTicket };
ser = new DataContractJsonSerializer(typeof(OGetModesRequest));
mem = new MemoryStream();
ser.WriteObject(mem, oGetModes);
webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
//Exception here
bData = webClient.UploadData("http://localhost:26104/Service.svc/GetModes", "POST", mem.ToArray());
stream = new MemoryStream(bData);
obj = new DataContractJsonSerializer(typeof(OGetModesResponse));
OGetModesResponse OResultModes = obj.ReadObject(stream) as OGetModesResponse;
I debug my services and works fine. What can be happening?
Thanks for help.
Edit (solution):
CTMode is a class used by managing object that I obtain using NHibernate so I create a new class serializable called CMode
[DataContract]
public class OGetModesResponse
{
[DataMember]
public string sTicket;
[DataMember]
public emStatus emStatus;
[DataMember]
public IList<CMode> aoModes;
}
[Serializable]
public class CMode
{
public Int32 nId;
public Int32 nCode;
public String sName;
}
Try to check inner exception and add some logging/ trace on the server.
There are few possibilities for your (generic) error as you may not be aware about inner exception:
object CTMode is missing DataContract, DataMember attribute.
object CTMode is an enum that missing attributes or has incorrect value that cannot be serialized
previous connection is not closed correctly
there is a proxy server on the way and you need to bypassed it

__type and inheritance with WCFService

[DataContract]
public abstract class BusMessage
{
[DataMember(Name = "encoding")]
public string Encoding { get; set; }
[DataMember(Name = "type")]
public virtual MessageType Type
{
get { return _type; }
private set { _type = value; }
}
}
[DataContract]
public class BusTextMessage : BusMessage
{
[DataMember(Name = "type")]
public override MessageType Type
{
get { return MessageType.Text; }
}
[DataMember(Name = "payload")]
public string Payload { get; set; }
}
[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(Helper))]
public interface ICommunicationService
{
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/SendMessage")]
string SendMessage(BusMessage jsonMessage);
}
}
When I send request with Postman chrome, if I don't add __type as "__type":"BusTextMessage:#TransportModels.Messages" the object won't be serialized properly because it doesn't know how to instantiate BusMessage class. I have already defined Type property which defines the type of message. Is there any possibility to override __type behaviour for example return proper implementation depending on Type property? I don't want anyone to put __type information to json manually so is there an option to edit json before deserialization and add __type property manually to json if it doesn't exist? For example I want to do something like this:
public void BeforeDeserialization(string json)
{
if(json doesnt include __type)
{
if(json property type(my property) is MessageType.Text)
add to json "__type":"BusTextMessage:#TransportModels.Messages"
///etc
}
}
I Found this methods but it doesn't seem to be usable:
[OnDeserializing()]
internal void OnDeserializingMethod(StreamingContext context)
{
}
I think you need to add the KnownType attribute to the BusMessage class.
[DataContract]
[KnownType(typeof(BusTextMessage)]
public class BusMessage
{
.
.
.
}
This is the quickest solution I discovered. I configure MessageInspector and handle AfterReceiveRequest. Then I check message format(XML,JSON). If it is XML(for example sent from any WCF Client written in C#, WCF is configured to send everything with XML's) then I accept that message because field __type will be automatically inserted by WCF mechanism. Otherwise I Check if it is JSON, for example sent from external client. If it doesn't contain property "__type" I check my property Type and generate proper __type value. For example if my Type is equal to Text I add __type property BusTextMessage:#TransportModels.Messages and insert it into JSON and then recreate the message. I couldn't find quicker and easier solution and it seems to be working. Handling AfterReceiveRequest I found at http://code.msdn.microsoft.com/windowsdesktop/WCF-REST-Message-Inspector-c4b6790b.
public class MessageTypeInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
RecreateMessage(ref request);
return null;
}
}
private void RecreateMessage(ref Message message)
{
WebContentFormat messageFormat = this.GetMessageContentFormat(message);
var ms = new MemoryStream();
XmlDictionaryWriter writer = null;
switch (messageFormat)
{
case WebContentFormat.Default:
case WebContentFormat.Xml:
writer = XmlDictionaryWriter.CreateTextWriter(ms);
break;
case WebContentFormat.Json:
writer = JsonReaderWriterFactory.CreateJsonWriter(ms);
break;
case WebContentFormat.Raw:
this.ReadRawBody(ref message);
break;
}
message.WriteMessage(writer);
writer.Flush();
string messageBody = Encoding.UTF8.GetString(ms.ToArray());
if (messageFormat == WebContentFormat.Json && !messageBody.Contains("__type"))
messageBody = AddTypeField(messageBody);
ms.Position = 0;
ms = new MemoryStream(Encoding.UTF8.GetBytes(messageBody));
XmlDictionaryReader reader = messageFormat == WebContentFormat.Json ?
JsonReaderWriterFactory.CreateJsonReader(ms, XmlDictionaryReaderQuotas.Max) :
XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
Message newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);
newMessage.Properties.CopyProperties(message.Properties);
message = newMessage;
}
private WebContentFormat GetMessageContentFormat(Message message)
{
WebContentFormat format = WebContentFormat.Default;
if (message.Properties.ContainsKey(WebBodyFormatMessageProperty.Name))
{
WebBodyFormatMessageProperty bodyFormat;
bodyFormat = (WebBodyFormatMessageProperty)message.Properties[WebBodyFormatMessageProperty.Name];
format = bodyFormat.Format;
}
return format;
}
private string AddTypeField(string jsonReply)
{
var typeRegex = new Regex("\"type\":(?<number>[0-9]*)");
Match match = typeRegex.Match(jsonReply);
if (match.Success)
{
int number = Int32.Parse(match.Groups["number"].Value);
var type = (MessageType)number;
var nameFormat = string.Format("Bus{0}Message", type);
string format = string.Format("\"__type\":\"{0}:#TransportModels.Messages\"", nameFormat);
jsonReply = "{" + string.Format("{0},{1}", format, jsonReply.Substring(1));
return jsonReply;
}
else
{
throw new Exception("Wrong message type.");
}
}

How to use a proxy with RedditSharp?

I'm using RedditSharp from https://github.com/SirCmpwn/RedditSharp in a script of mine, and I'm simply asking, when connecting using this how do I implement a proxy? and could I change the proxy midscript?
There no standalone way, you can't accomplish this without modifying this library source code.
So most painless-way:
Overload constructor of RedditSharp - add new argument with IWebAgent as type. So it will look like this:
public Reddit() : this(new WebAgent())
{
}
public Reddit(IWebAgent agent)
{
JsonSerializerSettings = new JsonSerializerSettings();
JsonSerializerSettings.CheckAdditionalContent = false;
JsonSerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
_webAgent = agent;
CaptchaSolver = new ConsoleCaptchaSolver();
}
Remove "sealed" keyword from RedditSharp.WebAgent class declaration.
Make RedditSharp.WebAgent.CreateRequest method virtual, so it will look like this:
public virtual HttpWebRequest CreateRequest(string url, string method, bool prependDomain = true)
{
...
}
Create your own WebAgent based on old-one:
public class MyAgent: WebAgent
{
public IWebProxy Proxy { get; set; }
public override HttpWebRequest CreateRequest(string url, string method, bool prependDomain = true)
{
var base_request = base.CreateRequest(url, method, prependDomain);
if (Proxy != null)
{
base_request.Proxy=Proxy;
}
return base_request;
}
}
Use it in your code:
var agent = new MyAgent();
var reddit = new Reddit(agent);
...
agent.Proxy = new WebProxy("someproxy.net", 8080);
So now you can set proxy anytime, from anywhere. Not tested really, but it must work.

Categories