Passing values in Header - wcf

We are consuming an external web service (WCF) in our AX2012 project. We followed the procedure described in the following blog.
We are implementing security by passing the token in the header. However, what i am not sure of is how to do this in AX2012.
the sample code for getting the token is
static void myTestServiceWSDL(Args _args)
{
myServiceWSDL.Proxies.Service.ServiceClient service;
myServiceWSDL.Proxies.Service.LoginData LoginData;
str token;
System.Exception ex;
System.Type type;
try
{
type = CLRInterop::getType('myServiceWSDL.Proxies.Service.ServiceClient');
service = AifUtil::createServiceClient(type);
LoginData = new myServiceWSDL.Proxies.Service.LoginData();
LoginData.set_uName("test");
LoginData.set_pwd("test");
token=service.Login(LoginData);
info(token);
}
catch(Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(CLRInterop::getAnyTypeForObject(ex.ToString()));
}
}
The token comes back fine which confirms the code is working.
Now the question is how to do i set header values for the message.
If it was C# i would have done
using (MemberMasterClient proxy = new MemberMasterClient())
{
using (OperationContextScope scope
= new OperationContextScope(proxy.InnerChannel))
{
// set the message in header
MessageHeader header =
MessageHeader.CreateHeader("SourceApplication",
"urn:spike.WCFHeaderExample:v1",
"WCFClient Application 2");
OperationContext.Current.OutgoingMessageHeaders.Add(header);
Console.WriteLine("Membership Details");
Console.WriteLine("Henry's - {0}", proxy.GetMembership("Henry"));
}
}
}
Could any one let me know how to do the equivalent in X++
One idea which has been on my mind is to write an assembly in C# which can then be called in AX2012. Will give that a go, but the idea is to code this in X++ in AX2012

The only thing you do differently in X++ is creating the proxy using the Aif utility. So basically, your C# example you listed, the only difference would be the proxy = new MemberMasterClient() which goes through AIF. All the other code you can take into X++ as-is (except for the "using"). You just need to have the right assemblies reference in the AOT, and use the full namespace in the code.
Alternatively, as you mentioned, you can just code it all in C# and call that from AX :-)

Related

What is the replacement of FormDataCollection in Asp.Net core?

I am trying to use FormDataCOllection in Asp.Net core web api project. As per the documentation it is not there in .net core.
How can I still use it? Or What has replaced it?
You can use the Form property of HttpContext.Request which will return an IFormCollection instance.
FormDataCollection is normally type of the information/class/model sent by forms or grid edit or sumbit links. You can simply use the string as input parameter and then using Json, convert it to the class type you've expected.
Here is an example:
public string NewUser(string values)
{
var message = "";
try
{
var newUser = new User_Detail();
JsonConvert.PopulateObject(values, newUser);
db.User_Detail.Add(newUser);
db.SaveChanges();
message = "User cretaed successfully";
}
catch (Exception ex)
{
message = "An error happened in this method.";
}
return JsonConvert.SerializeObject(message);
}
So, PopulateObject converts your input string (values in this example) to the class you've expected, something similar to FormDataCollection you've needed.
Note: JsonConvert is in Newtonsoft.Json namespace

Issue while running web service using batch job?

I have consumed a web service using visual studio and used managed code to call that in AX 2012.
Now if I am running the code in a simple job as:
static void CurrencyService(Args _args)
{
CurrencyConvert.Currency_Convert.CurrencyServiceClient convertcurrency;
CurrencyConvert.Currency_Convert.Currency currency;
System.ServiceModel.Description.ServiceEndpoint endPoint;
System.Type type;
System.Exception ex;
str s1;
try
{
type = CLRInterop::getType('CurrencyConvert.Currency_Convert.CurrencyServiceClient');
convertcurrency = AifUtil::createServiceClient(type);
endPoint = convertcurrency.get_Endpoint();
// endPoint.set_Address(new System.ServiceModel.EndpointAddress("http://localhost/HelloWorld"));
currency = convertcurrency.GetConversionRate(CurrencyConvert.Currency_Convert.CurrencyCode::AUD,CurrencyConvert.Currency_Convert.CurrencyCode::INR );
info(strFmt('%1', CLRInterop::getAnyTypeForObject(currency.get_Rate())));
}
catch(Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(CLRInterop::getAnyTypeForObject(ex.ToString()));
}
}
Above job is working fine and producing results in a infolog.
Now, if a same piece of code is written under a class for batchjob(extending Runbasebatch class) as we normally do for any batch job, it is throwing an error as:
Microsoft.Dynamics.Ax.Xpp.ErrorException: Exception of type
'Microsoft.Dynamics.Ax.Xpp.ErrorException' was thrown.
at Dynamics.Ax.Application.BatchRun.runJobStatic(Int64 batchId) in
BatchRun.runJobStatic.xpp:line 38
at BatchRun::runJobStatic(Object[] )
at
Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type
type, String MethodName, Object[] parameters)
at BatchIL.taskThreadEntry(Object threadArg)
Other batch jobs except which used web services are working properly.
I have already tried many things such as : RunOn property of a class is set as "server" etc.
This is the case with each web service we have consumed.
Does anybody have a proper solution for this??
I am assuming that this is the same as on the Dynamics Ax community site post. So reading there, the error is not related to batch but to the following: "Could not find default endpoint element that references contract 'Currency_Convert.ICurrencyService' in the ServiceModel client configuration section.
This is because the endpoint is being searched in the AX32.exe.config file and this is not the one you need. You need to get it from the config file associated with your DLL.
To do this, you need to construct you client differently in AX. You need to use the AIF util because that way, the right config is used. Example:
type= CLRInterop::getType('DynamicsAxServices.WebServices.ZipCode.USAZipCodeServiceRef.PostalCodeServiceClient');
postalServiceClient = AifUtil::createServiceClient(type);
Apart from that, there is also an extra thing to whatch for. Separate environments would require different URL's and this can be solved by manually specifying your endpoint address and let it use a system parameter. (that way you can specify different configurations for DEV/TEST/PROD) (Note: below the endpoint address is hard coded and that should be a parameter)
static void Consume_GetZipCodePlaceNameWithEndPoint(Args _args)
{
DynamicsAxServices.WebServices.ZipCode.USAZipCodeServiceRef.PostalCodeServiceClient postalServiceClient;
DynamicsAxServices.WebServices.ZipCode.USAZipCodeServiceRef.PostalCodepostalCode;
System.ServiceModel.Description.ServiceEndpointendPoint;
System.ServiceModel.EndpointAddressendPointAddress;
System.Exceptionexception;
System.Typetype;
;
try
{
// Get the .NET type of the client proxy
type = CLRInterop::getType('DynamicsAxServices.WebServices.ZipCode.USAZipCodeServiceRef.PostalCodeServiceClient');
// Let AifUtil create the proxy client because it uses the VSAssemblies path for the config file
postalServiceClient = AifUtil::createServiceClient(type);
// Create and endpoint address, This should be a parameter stored in the system
endPointAddress = new System.ServiceModel.EndpointAddress ("http://www.restfulwebservices.net/wcf/USAZipCodeService.svc");
// Get the WCF endpoint
endPoint = postalServiceClient.get_Endpoint();
// Set the endpoint address.
endPoint.set_Address(endPointAddress);
// Use the zipcode to find a place name
postalCode = postalServiceClient. GetPostCodeDetailByPostCode("10001"); // 10001 is New York
// Use the getAnyTypeForObject to marshal the System.String to an Ax anyType
// so that it can be used with info()
info(strFmt('%1', CLRInterop::getAnyTypeForObject(postalCode.get_ PlaceName())));
}
catch(Exception::CLRError)
{
// Get the .NET Type Exception
exception = CLRInterop::getLastException();
// Go through the inner exceptions
while(exception)
{
// Print the exception to the infolog
info(CLRInterop::getAnyTypeForObject(exception.ToString()));
// Get the inner exception for more details
exception = exception.get_InnerException();
}
}
}
I was getting the same issue, finally its resolved.
Login to AOS machine with AOS service account and check if you can browse internet. If not then you need to set proxy for internet in IE.
So basically under AOS account, process could not connect to Webservice provider.
I have resolved this issue. I just end session all online user and stop/start AOS after doing ful cil. Maybe deleting XPPIL and Appl files helps before start the AOS service.

Providing workflow extensions to a workflow service - WF 4.0

Greetings one and all!
I'm new to WF 4.0 and WWF in general so forgive me if this seems like a newbie type of question, but believe me I've scoured the depths of the Internet for a solution to this problem, but to no avail.
I have created a sample WF application with a custom CodeActivity that requires an extension be provided, as per below:
public sealed class PreparePizza : CodeActivity
{
public InArgument<Order> Order { get; set; }
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
base.CacheMetadata(metadata);
if (this.Order == null)
metadata.AddValidationError("You must supply an Order.");
metadata.RequireExtension<IPreparePizzaExtension>();
}
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override void Execute(CodeActivityContext context)
{
// Obtain the runtime value of the Text input argument
Order order = context.GetValue(this.Order);
var extension = context.GetExtension<IPreparePizzaExtension>();
extension.Prepare(order);
}
}
public interface IPreparePizzaExtension
{
void Prepare(Order order);
}
I then slot this activity into a workflow service and attempt to consume via my web app by adding a service reference. However, when I add the reference I get:
System.Activities.ValidationException: An extension of type 'PizzaMan.ActivityLibrary.IPreparePizzaExtension' must be configured in order to run this workflow.
Fair enough - of course my activity requires that I pass it an implementation of IPreparePizzaExtension - after all, I've told it to!
So my question is, how on earth do I pass this to the service? I can manage this easily enough in a console app scenario, using the WorkflowInvoker, but I cannot see any obvious way to do this via the service approach. I would assume that obviously a programmatic approach to adding the reference is what's needed, but again I'm at a loss as to precisely how to go about this.
Any help would be greatly appreciated.
Best regards
Ian
The WorkflowServiceHost has a WorkflowExtensions property where you can add the workflow extenstion. There are several ways you can do that. If you are self hosting this is easy as you create the WorkflowServiceHost. If you are usign IIS you need to create a ServiceHostFactory to configure you WorkflowServiceHost. Finally there is an option to add the workflow extension in the CacheMetadata of your activity using the metadata.AddDefaultExtensionProvider() function.
Solved it as follows, self-hosting style:
static void Main(string[] args)
{
Workflow1 workflow = new Workflow1();
// Provide some default values; note: these will be overriden once method on the service is called.
workflow.productID = -1;
Uri address = new Uri("http://localhost:1234/WorkflowService1");
WorkflowServiceHost host = new WorkflowServiceHost(workflow, address);
// Behaviours
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
host.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
// Persistence
var connStr = #"";
var behavior = new SqlWorkflowInstanceStoreBehavior(connStr);
behavior.InstanceCompletionAction = InstanceCompletionAction.DeleteNothing;
behavior.InstanceLockedExceptionAction = InstanceLockedExceptionAction.AggressiveRetry;
behavior.InstanceEncodingOption = InstanceEncodingOption.None;
host.Description.Behaviors.Add(behavior);
// Add extension implementations
if (!TEST_MODE)
{
host.WorkflowExtensions.Add(new MyExtension());
}
else
{
host.WorkflowExtensions.Add(new MyExtensionTest());
}
host.Faulted += new EventHandler(host_Faulted);
host.Open();
foreach (System.ServiceModel.Description.ServiceEndpoint endpoint in host.Description.Endpoints)
{
Console.WriteLine(endpoint.Address);
}
Console.WriteLine("Listening...");
Console.ReadLine();
host.Close();
}
My toolkit has configuration support for this. See http://neovolve.codeplex.com/wikipage?title=Neovolve.Toolkit.Workflow.dll%20-%201.1
There is also this method of doing things:
http://wf.codeplex.com/wikipage?title=How%20do%20I%20add%20an%20extension%20to%20a%20WCF%20Workflow%20Service?

Get calling assembly name inside WCF service

I'm trying to achieve the following:
whenever a call to service is performed I want to be able to identify the client.
I thought about getting the calling assembly name by iterating over stack trace
but I failed to get the client assembly name.
Sample code:
private List<System.Reflection.Assembly> GetCallingAssemblies()
{
List<System.Reflection.Assembly> assemblies = new List<System.Reflection.Assembly>();
StackTrace stackTrace = new StackTrace(0, true);
for (int i = 0; i < stackTrace.FrameCount; i++)
{
StackFrame stackFrame = stackTrace.GetFrame(i);
System.Reflection.MethodBase methodBase = stackFrame.GetMethod();
Type type = methodBase.ReflectedType;
System.Reflection.Assembly assembly;
if (type != null)
{
assembly = System.Reflection.Assembly.GetAssembly(type);
if (assemblies.Contains(assembly) == false)
{
assemblies.Add(assembly);
}
}
}
return assemblies;
}
I must be missing something: you're trying to identify the client through assemblies? Why not use authentication?
Besides, who says the client even has assemblies? It may be a Java client, or some other platform.
When your client calls a WCF service, all that goes between the two is the serialized message - the method to call and all the parameters to pass in.
There is no other connection at runtime between server and client. The server cannot "reach back" and look at the client - there is no connection.
All your service can look at is the serialized message, and any message headers. So if you really really need this (what do you need it for??) then you need to make sure the client puts a marker / identification of some sort as a message header into the call.

Creating an SPListItem in a WCF service deployed to SharePoint

i have the following method in a WCF service, that has been deployed to SharePoint using Shail Malik's guide:
[OperationContract]
public string AddItem(string itemTitle, Guid? idOfListToUse)
{
using (var portal = new SPSite(SPContext.Current.Site.Url, SPContext.Current.Site.SystemAccount.UserToken))
{
using (var web = portal.OpenWeb())
{
Guid listId;
web.AllowUnsafeUpdates = true;
if (idOfListToUse != null && idOfListToUse.Value != new Guid())
{
listId = idOfListToUse.Value;
}
else
{
try
{
listId = new Guid(web.Properties[PropertyBagKeys.TagsList]);
}
catch (Exception ex)
{
throw new MyException("No List Id for the tag list (default list) has been found!", ex);
}
}
var list = web.Lists[listId];
string title = "";
SPSecurity.RunWithElevatedPrivileges(delegate{
var newItem = list.Items.Add();
newItem["Title"] = itemTitle;
newItem.Update();
title = newItem.Title;
});
web.AllowUnsafeUpdates = false;
return title;
}
}
}
When the method gets called from Javascript (using Rick Strahl's excellent ServiceProxy.js) it fails and it does so on newItem.Update() because of ValidateFormDigest().
Here's the kicker though, when I step through the code it works! No exceptions at all!
Ok, found the answer (there's 2 even :-D)
First, the dirty one:
Set FormDigestValidatedProperty in the context:
HttpContext.Current.Items["FormDigestValidated"] = true;
Second, the slightly less dirty version (basically leaving the way open for XSS attacks, but this is an intranet anyway)
The answer
I don't think you can access 'list' as it was created outside the elevated code block.
http://blogs.pointbridge.com/Blogs/herzog_daniel/Pages/Post.aspx?_ID=8
I'm guessing when you are stepping though the entire process is in admin mode so all are elevated.
Colin, it's a really bad idea to try to access HttpContext (likewise SPContext) inside a WCF service. See here: MSDN: WCF Services and ASP.NET
From the article:
HttpContext: Current is always null
when accessed from within a WCF
service.
It's likely this is the cause of your problem.
EDIT: I notice that you're trying to use SPContext to get the url of the site collection. I didn't find a good solution to this either so I just send the url of the target site collection as a parameter to the service call. Not the most optimal solution but I couldn't think of a better way. Also, if you need to check authentication/identities, etc use ServiceSecurityContext.Current.