how to call my api qnamaker using Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker nuget version 1.0.0 - qnamaker

I am getting started with Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker nuget,
and I am trying to use QnAMakerClient() class to initialise a new instance of the QNAMakerClient class.
But this class takes abstract parameters :
public QnAMakerClient(ServiceClientCredentials credentials, params DelegatingHandler[] handlers).
I found some solution
https://csharp.hotexamples.com/examples/Microsoft.Rest/TokenCredentials/-/php-tokencredentials-class-examples.html which indicates how to get the credentials token.
As I am new to this concept, so I don't know how to get credentials token for my ServiceClientCredentials.

You can create your own TokenCredentials class which inherit from ServiceClientCredentials as shown below:
public class TokenCredentials : ServiceClientCredentials
{
/// <summary>
/// The bearer token type, as serialized in an http Authentication header.
/// </summary>
private const string BearerTokenType = "Bearer";
/// <summary>
/// Gets or sets secure token used to authenticate against Microsoft Azure API.
/// No anonymous requests are allowed.
/// </summary>
protected ITokenProvider TokenProvider { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="TokenCredentials"/>
/// class with the given 'Bearer' token.
/// </summary>
/// <param name="token">Valid JSON Web Token (JWT).</param>
public TokenCredentials(string token)
: this(token, BearerTokenType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TokenCredentials"/>
/// class with the given token and token type.
/// </summary>
/// <param name="token">Valid JSON Web Token (JWT).</param>
/// <param name="tokenType">The token type of the given token.</param>
public TokenCredentials(string token, string tokenType)
: this(new StringTokenProvider(token, tokenType))
{
if (string.IsNullOrEmpty(token))
{
throw new ArgumentNullException("token");
}
if (string.IsNullOrEmpty(tokenType))
{
throw new ArgumentNullException("tokenType");
}
}
/// <summary>
/// Create an access token credentials object, given an interface to a token source.
/// </summary>
/// <param name="tokenProvider">The source of tokens for these credentials.</param>
public TokenCredentials(ITokenProvider tokenProvider)
{
if (tokenProvider == null)
{
throw new ArgumentNullException("tokenProvider");
}
this.TokenProvider = tokenProvider;
}
/// <summary>
/// Apply the credentials to the HTTP request.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>
/// Task that will complete when processing has completed.
/// </returns>
public async override Task ProcessHttpRequestAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
if (TokenProvider == null)
{
throw new InvalidOperationException(Resources.TokenProviderCannotBeNull);
}
request.Headers.Authorization = await TokenProvider.GetAuthenticationHeaderAsync(cancellationToken);
await base.ProcessHttpRequestAsync(request, cancellationToken);
}
}
This is a good starting point for knowing more about QnAMaker.
Hope this helps!!!

Related

WCFFaultException with an error status code is always being returned back to the client as 202 accepted

In our application, if required, we are throwing a fault exception as follows:
throw new WebFaultException(new RequestFaultInfo(errorMessage), System.Net.HttpStatusCode.BadRequest);
The RequestFaultInfo class is as follows:
/// <summary>
/// Fault information used as the detail of a web service request failure.
/// </summary>
[DataContract]
public class RequestFaultInfo
{
#region Constants
/// <summary>
/// Summary fault reason.
/// </summary>
public const string Reason = "Request failure";
#endregion
private string m_errorDescription;
#region Object Lifetime
/// <summary>
/// Constructor specifying the login error description.
/// </summary>
/// <param name="errorDescription">Request error description.</param>
/// <exception cref="ArgumentException">errorDescription is null or empty.</exception>
public RequestFaultInfo(string errorDescription)
{
if (String.IsNullOrEmpty(errorDescription))
{
throw new ArgumentException("errorDescription cannot be null or empty", "errorDescription");
}
m_errorDescription = errorDescription;
}
#endregion
#region Properties
/// <summary>
/// Login error description.
/// </summary>
[DataMember]
public string ErrorDescription
{
get { return m_errorDescription; }
private set { m_errorDescription = value; }
}
#endregion
However, the client is always getting the status code 202 (Accepted). In this case the client is Postman on the same server.
Has anyone got any ideas why this is happening?

Implementing bcrypt in .NET Core 2.0

I am new to developing with Core. I created a ASP.NET Core web application (MVC) in Visual Studio with Individual User accounts stored in app. I created a database for the app in SQL server, updated the connection string, and ran Update-Database in the NuGet console. I would like to override the password hashing function and instead use bcrypt to hash. I was hoping to use the BCrypt-Core, BCrypt.Net - Next, or Bcrypt-Official package. But I dont know where to go from there to ensure that the hashing is overridden when the password is generated and when the user logs in. My guess is that I need to override PasswordHasher but what methods do I need to override & what about when the user wants to log in? Any advice / suggestions / links to current implementations would be appreciated!
Create a class called BCryptPasswordHasher.cs
public class BCryptPasswordHasher<TUser> : PasswordHasher<TUser> where TUser : class
{
/// <summary>
/// Overrides instance of Microsoft.AspNetCore.Identity.PasswordHasher
/// </summary>
/// <param name="optionsAccessor"></param>
public BCryptPasswordHasher(IOptions<PasswordHasherOptions> optionsAccessor = null)
{
}
/// <summary>
/// Returns a hashed representation of the supplied password for the specified user.
/// </summary>
/// <param name="user"></param>
/// <param name="password"></param>
/// <returns></returns>
public override string HashPassword(TUser user, string password)
{
return BCrypt.Net.BCrypt.HashPassword(password);
}
/// <summary>
/// Returns a Microsoft.AspNetCore.Identity.PasswordVerificationResult indicating
// the result of a password hash comparison.
/// </summary>
/// <param name="user"></param>
/// <param name="hashedPassword">The hash value for a user's stored password.</param>
/// <param name="providedPassword"> The password supplied for comparison.</param>
/// <returns></returns>
public override PasswordVerificationResult VerifyHashedPassword(TUser user, string hashedPassword, string providedPassword)
{
if (hashedPassword == null) { throw new ArgumentNullException(nameof(hashedPassword)); }
if (providedPassword == null) { throw new ArgumentNullException(nameof(providedPassword)); }
if (BCrypt.Net.BCrypt.Verify(providedPassword, hashedPassword))
{
return PasswordVerificationResult.Success;
}
else
{
return PasswordVerificationResult.Failed;
}
}
}
In Startup.cs - BEFORE AddIdentity add
services.AddScoped<IPasswordHasher<ApplicationUser>, BCryptPasswordHasher<ApplicationUser>>();
Thanks to Andrew Lock for getting me 90% of the way there. https://andrewlock.net/migrating-passwords-in-asp-net-core-identity-with-a-custom-passwordhasher/

Workflow services scalability issue

I'm currently experiencing some issues with workflow services.
They work fine if I start 4, 5 in short sequence, but if I increase this value (starting from ~10) then I get the following exception:
This channel can no longer be used to send messages as the output session was auto-closed due to a server-initiated shutdown. Either disable auto-close by setting the DispatchRuntime.AutomaticInputSessionShutdown to false, or consider modifying the shutdown protocol with the remote server.
I think that the problem is in the way I create proxies. I use the following code to provide proxies, attempting to reuse existing ones:
public abstract class ProxyProvider<TService>
where TService : class
{
/// <summary>
/// Static reference to the current time provider.
/// </summary>
private static ProxyProvider<TService> current = DefaultProxyProvider.Instance;
private TService service;
/// <summary>
/// Gets or sets the current time provider.
/// </summary>
/// <value>
/// The current time provider.
/// </value>
public static ProxyProvider<TService> Current
{
get
{
return ProxyProvider<TService>.current;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
ProxyProvider<TService>.current = value;
}
}
/// <summary>
/// Resets to default.
/// </summary>
public static void ResetToDefault()
{
ProxyProvider<TService>.current = DefaultProxyProvider.Instance;
}
/// <summary>
/// Loads the proxy.
/// </summary>
/// <param name="forceNew">if set to <c>true</c> [force new].</param>
/// <returns>The instance of the proxy.</returns>
public virtual TService Provide(bool forceNew = false)
{
if (forceNew || !this.IsInstanceValid())
{
this.service = this.CreateInstance();
return this.service;
}
return this.service;
}
/// <summary>
/// Internals the load.
/// </summary>
/// <returns>The new created service.</returns>
protected abstract TService CreateInstance();
private bool IsInstanceValid()
{
var instance = this.service as ICommunicationObject;
if (instance == null)
{
return false;
}
return instance.State != CommunicationState.Faulted && instance.State != CommunicationState.Closed && instance.State != CommunicationState.Closing;
}
/// <summary>
/// Defines the default <see cref="ProxyProvider<TService>"/> which uses the System DateTime.UtcNow value.
/// </summary>
private sealed class DefaultProxyProvider : ProxyProvider<TService>
{
/// <summary>
/// Reference to the instance of the <see cref="ProxyProvider<TService>"/>.
/// </summary>
private static ProxyProvider<TService> instance;
/// <summary>
/// Gets the instance.
/// </summary>
public static ProxyProvider<TService> Instance
{
get
{
if (DefaultProxyProvider.instance == null)
{
DefaultProxyProvider.instance = new DefaultProxyProvider();
}
return DefaultProxyProvider.instance;
}
}
/// <summary>
/// Loads the specified force new.
/// </summary>
/// <returns>A non-disposed instance of the given service.</returns>
protected override TService CreateInstance()
{
var loadedService = Activator.CreateInstance<TService>();
return loadedService;
}
}
With an additional "lazy" provider:
public class CustomConstructorProxyProvider<TService> : ProxyProvider<TService>
where TService : class
{
private readonly Func<TService> constructor;
/// <summary>
/// Initializes a new instance of the <see cref="CustomConstructorProxyProvider<TService>"/> class.
/// </summary>
/// <param name="constructor">The constructor.</param>
public CustomConstructorProxyProvider(Func<TService> constructor)
{
this.constructor = constructor;
}
/// <summary>
/// Internals the load.
/// </summary>
/// <returns>The new created service.</returns>
protected override TService CreateInstance()
{
var service = this.constructor();
return service;
}
}
Used this way:
var proxy = ProxyProvider<IWorkflowService>.Current.Provide();
proxy.DoSomething();
Initialized like this:
ProxyProvider<IWorkflowService>.Current = new CustomConstructorProxyProvider<IWorkflowService>(() => new WorkflowServiceProxy("endpoint"));
Workflow services are hosted by IIS and I added the following throttling settings:
<serviceThrottling
maxConcurrentCalls="512"
maxConcurrentInstances="2147483647"
maxConcurrentSessions="1024"/>
which should be enough for my needs.
I hope that someone can help me configuring client and server to have achieve the desired scalability (a few hundreds started in sequence and running in parallel, using the WorkflowInstance sql store).
UPDATE:
I'm using NetTcpBinding for all services.
UPDATE 2:
All services are hosted and consumed by now locally.
Thanks
Francesco

Content-Type header value missing after upgrade to WCF Data Services 5.0

After upgrading to WCF Data Services 5.0 for OData V3 i can't make insert or updates.
The only thing that is possible are selects.
EDIT
Exception in overridden method HandleException:
Exception:
System.Data.Services.DataServiceException: Content-Type header value missing.
at System.Data.Services.HttpProcessUtility.ReadContentType(String contentType, String& mime, Encoding& encoding)
at System.Data.Services.Serializers.Deserializer.CreateDeserializer(RequestDescription description, IDataService dataService, Boolean update, UpdateTracker tracker)
at System.Data.Services.DataService1.HandlePostOperation(RequestDescription description, IDataService dataService)
at System.Data.Services.DataService1.ProcessIncomingRequest(RequestDescription description, IDataService dataService)
at System.Data.Services.DataService1.HandleNonBatchRequest(RequestDescription description)
at System.Data.Services.DataService1.HandleRequest()
ResponseStatusCode: 400
Client-Code
I set a token in the HTTP-Request-Header...
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBase"/> class.
/// </summary>
/// <param name="uri">service uri</param>
protected ServiceBase(Uri uri)
{
this.Context = new Entities(uri) { MergeOption = MergeOption.OverwriteChanges };
this.Context.SendingRequest += new EventHandler<SendingRequestEventArgs>(this.ContextSendingRequest);
}
/// <summary>
/// OnSendingRequest
/// </summary>
/// <param name="sender">source</param>
/// <param name="e">event args</param>
private void ContextSendingRequest(object sender, SendingRequestEventArgs e)
{
e.RequestHeaders["token"] = "xyassdfdfdfdf";
}
Service-Code:
The Service parses this Header and checks the token
public XYDataService()
{
this.ProcessingPipeline.ProcessingRequest += this.ProcessingPipelineProcessingRequest;
}
/// <summary>
/// OnProcessingRequest
/// </summary>
/// <param name="sender">source</param>
/// <param name="e">event args</param>
private void ProcessingPipelineProcessingRequest(object sender, DataServiceProcessingPipelineEventArgs e)
{
var authHeader = HttpContext.Current.Request.Headers["token"];
if (string.IsNullOrEmpty(authHeader) || !authHeader.Equals("xyassdfdfdfdf"))
{
throw new DataServiceException(401, "401 Unauthorized");
}
}
Thanks for your help.
Andi
Using answer since I need to put more text here.
I tried a very similar code to yours, but it works just fine:
DemoService ctx = new DemoService(new Uri("http://services.odata.org/(S(jcemln1vp0u1gqtoyqqpwrc1))/OData/OData.svc/"));
ctx.UsePostTunneling = true;
ctx.SendingRequest += (sender, ea) => { ea.RequestHeaders["token"] = "value"; };
ctx.UpdateObject(ctx.Products.First());
ctx.SaveChanges();
Running this using the latest WCF DS 5.0 bits from NuGet works. I also verified with Fiddler that it does send the Content-Type header.
Your request on the other hand doesn't have the Content-Type header. Can you please verify that you're using the latest NuGet packages and that the code you posted above is really what you're testing?

WCF Routing with custom filter: No matching MessageFilter was found for the given message

Im writing a routing service for some WCF services. I want to use a custom message filter that filters out my message.
Here is my custom message filter:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Dispatcher;
using System.IO;
using System.Xml;
namespace AMA_ISE.Router.MessageFilter
{
/// <summary>
/// This message filter implements the custom filtering for the routing services to re-route the incoming messages
/// correctly.
/// </summary>
public class CustomMessageFilter : System.ServiceModel.Dispatcher.MessageFilter
{
/// <summary>
/// The sales channel for which the filter is configured.
/// </summary>
private string _salesChannel;
/// <summary>
/// The service, for which the filter is configured.
/// </summary>
private string _service;
/// <summary>
/// Initializes a new instance of the <see cref="CustomMessageFilter"/> class.
/// </summary>
/// <param name="messageData">The message data.</param>
public CustomMessageFilter(string messageData)
{
var splitted = messageData.Split("|".ToCharArray());
_salesChannel = splitted[0];
_service = splitted[1];
}
/// <summary>
/// When overridden in a derived class, tests whether a message satisfies the filter criteria. The body cannot be examined.
/// </summary>
/// <param name="message">The <see cref="T:System.ServiceModel.Channels.Message"/> object to test.</param>
/// <returns>
/// true if the <see cref="T:System.ServiceModel.Channels.Message"/> object satisfies the filter criteria; otherwise, false.
/// </returns>
public override bool Match(System.ServiceModel.Channels.Message message)
{
return true;
}
/// <summary>
/// When overridden in a derived class, tests whether a buffered message satisfies the criteria of a filter.
/// </summary>
/// <param name="buffer">The <see cref="T:System.ServiceModel.Channels.MessageBuffer"/> object to test.</param>
/// <returns>
/// true if the <see cref="T:System.ServiceModel.Channels.MessageBuffer"/> object satisfies the filter criteria; otherwise, false.
/// </returns>
public override bool Match(System.ServiceModel.Channels.MessageBuffer buffer)
{
using (var memStream = new MemoryStream())
{
var msgText = GetMessageEnvelope(buffer);
// check sales channel
if (GetValueByTagName(msgText, "SalesChannel") != _salesChannel)
return false;
// check the requested service
var toValue = GetValueByTagName(msgText, "To");
File.AppendAllText("C:\\temp\\filter.txt", toValue + " - " + _service + " - " + toValue.Trim().EndsWith(_service) + "\r\n");
return toValue.Trim().EndsWith(_service);
}
}
/// <summary>
/// Gets the message envelope.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns></returns>
private static string GetMessageEnvelope(System.ServiceModel.Channels.MessageBuffer buffer)
{
using (MemoryStream memStream = new MemoryStream())
{
var msg = buffer.CreateMessage();
XmlDictionaryWriter xmlDictWriter =
XmlDictionaryWriter.CreateTextWriter(memStream);
msg.WriteMessage(xmlDictWriter);
xmlDictWriter.Flush();
memStream.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(memStream);
var text = reader.ReadToEnd();
return text;
}
}
/// <summary>
/// Gets the name of the value by tag.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="tagName">Name of the tag which content we are searching for.</param>
/// <returns></returns>
private static string GetValueByTagName(string text, string tagName)
{
var txt = text.Substring(text.IndexOf("<" + tagName+" ") + 1);
txt = txt.Substring(txt.IndexOf(">") + 1);
return txt.Substring(0, txt.IndexOf("<"));
}
}
}
As you can see, Im writing some information in a log file located in c:\temp. I expect to match the current filter when Match() returns true; that is not happening. When using this filter, I have the following text file:
http://localhost/AMA-ISE.Router/BookingService.svc - RetrieveService.svc - False
http://localhost/AMA-ISE.Router/BookingService.svc - CancelService.svc - False
http://localhost/AMA-ISE.Router/BookingService.svc - BookingService.svc - True
http://localhost/AMA-ISE.Router/BookingService.svc - AvailService.svc - False
http://localhost/AMA-ISE.Router/BookingService.svc - RebookingService.svc - False
That Looks nice, the BookingService.svc does fits and Match() will give true back. But it doesnt work. When I now use the routing service, I get the following error:
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: No matching MessageFilter was found for the given Message.
Whats wrong? Have I misunderstood something with the message filters? Please help me!
Michael Baarz
not all XML are formed in the same manner. Sometimes tags containing namespaces directly and sometimes the namespace is prefixed and used. That was why my method has not worked. Here is the working code for a custom filter manager (maybe someone is interested in):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Dispatcher;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace AMA_ISE.Router.MessageFilter
{
/// <summary>
/// This message filter implements the custom filtering for the routing services to re-route the incoming messages
/// correctly.
/// </summary>
public class CustomMessageFilter : System.ServiceModel.Dispatcher.MessageFilter
{
/// <summary>
/// The sales channel for which the filter is configured.
/// </summary>
private string _salesChannel;
/// <summary>
/// The service, for which the filter is configured.
/// </summary>
private string _service;
/// <summary>
/// Initializes a new instance of the <see cref="CustomMessageFilter"/> class.
/// </summary>
/// <param name="messageData">The message data.</param>
public CustomMessageFilter(string messageData)
{
var splitted = messageData.Split("|".ToCharArray());
_salesChannel = splitted[0];
_service = splitted[1];
}
/// <summary>
/// When overridden in a derived class, tests whether a message satisfies the filter criteria. The body cannot be examined.
/// </summary>
/// <param name="message">The <see cref="T:System.ServiceModel.Channels.Message"/> object to test.</param>
/// <returns>
/// true if the <see cref="T:System.ServiceModel.Channels.Message"/> object satisfies the filter criteria; otherwise, false.
/// </returns>
public override bool Match(System.ServiceModel.Channels.Message message)
{
return true;
}
static int counter = 0;
/// <summary>
/// When overridden in a derived class, tests whether a buffered message satisfies the criteria of a filter.
/// </summary>
/// <param name="buffer">The <see cref="T:System.ServiceModel.Channels.MessageBuffer"/> object to test.</param>
/// <returns>
/// true if the <see cref="T:System.ServiceModel.Channels.MessageBuffer"/> object satisfies the filter criteria; otherwise, false.
/// </returns>
public override bool Match(System.ServiceModel.Channels.MessageBuffer buffer)
{
using (var memStream = new MemoryStream())
{
var document = GetMessageEnvelope(buffer);
var salesChannel = GetValueOfNamedNode(document.Root, "SalesChannel");
var toService = GetValueOfNamedNode(document.Root, "To");
return salesChannel == _salesChannel && toService.EndsWith(_service);
}
}
private static string GetValueOfNamedNode(XElement element, string findName)
{
if (element.Name.LocalName == findName)
return element.Value;
foreach (var ele in element.Elements())
{
var foundValue = GetValueOfNamedNode(ele, findName);
if (foundValue != null)
return foundValue;
}
return null;
}
/// <summary>
/// Gets the message envelope.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns></returns>
private static XDocument GetMessageEnvelope(System.ServiceModel.Channels.MessageBuffer buffer)
{
using (MemoryStream memStream = new MemoryStream())
{
var msg = buffer.CreateMessage();
XmlDictionaryWriter xmlDictWriter =
XmlDictionaryWriter.CreateTextWriter(memStream);
msg.WriteMessage(xmlDictWriter);
xmlDictWriter.Flush();
memStream.Seek(0, SeekOrigin.Begin);
var doc = XDocument.Load(memStream);
return doc;
}
}
}
}